最后一个单词的长度
给定一个字符串, 包含大小写字母、空格' '
,请返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0
。
注意事项
一个单词的界定是,由字母组成,但不包含任何的空格。
样例
给定 s = "Hello World"
,返回 5
。
/*
* @param s: A string
* @return: the length of last word
*/
public int lengthOfLastWord(String s) {
// write your code here
int length=s.length();
boolean b=s.contains(" ");
if(b==false){
return length;
}else{
String a[]=s.split(" ");
return a[a.length-1].length();
}
}}