Given a string s consists of upper/lower-case alphabets and empty space characters '
', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World",
return 5.
题目:这应该是碰到过的最简单的一道题目了。
思路:直接用spilt方法分割即可。
public int lengthOfLastWord(String s) {
String[] str = s.split(" ");
if(str.length > 0) {
return str[str.length-1].length();
}
return 0;
}

本文介绍了一种简单的方法来获取给定字符串中最后一个单词的长度。通过使用split方法分割字符串,可以有效地找到最后一个单词并返回其长度。

被折叠的 条评论
为什么被折叠?



