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
.
PS: str.split(" ") 代表将字符串以“ ”分割。
需要考虑要是分割后没有数组的情况
class Solution {
public int lengthOfLastWord(String s) {
String[] str = s.split(" ");
int len = str.length;
if(len<=0)
return 0;
char[] array = str[len-1].toCharArray();
return array.length;
}
}
http://blog.csdn.net/carssister/article/details/46835027
以上链接,里面写了java字符串分割的三种方法