58. Length of Last Word —— Java

来源:互联网 发布:阿里巴巴阿里云事业部 编辑:程序博客网 时间:2024/06/07 02:40

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字符串分割的三种方法

原创粉丝点击