58. 最后一个单词的长度

package leetcode;

public class demo_58 {
    public int lengthOfLastWord(String s) {
        String [] str=s.split(" ");
        if(s=="") {return 0;}
        try {
            if(str[str.length-1]!="") {
                return str[str.length-1].length();
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return 0;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        demo_58 d58=new demo_58();
        System.out.println(d58.lengthOfLastWord("hello world"));
    }

}

 

posted on 2021-04-02 16:44  一仟零一夜丶  阅读(45)  评论(0)    收藏  举报