[ 问题: ]
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.
给你一个字符串,设法获取它最后一个单词的长度。如果这个单词不存在,则返回0。
[ 分析 : ]A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World",
return 5.
[ 解法: ]
public class Solution { public int lengthOfLastWord(String s) { if (s != null && !s.trim().equals("")) { String[] arr = s.trim().split(" "); int length = arr[arr.length - 1].length(); return length; } return 0; } public static void main(String[] args) { String s = "leet code"; System.out.println(new Solution().lengthOfLastWord(s)); } }
更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, return
, 单词
, public
, length
一个
length of last word、last stick length、length of stay、the length of、length of time,以便于您获取更多的相关知识。