Length of Last Word

 1 public class Solution {
 2     /**
 3      * @param s A string
 4      * @return the length of last word
 5      */
 6     public static int lengthOfLastWord(String s) {
 7         if (s == null || s.isEmpty()){
 8             return 0;
 9         }
10         s = s.trim();
11         int len = s.length();
12         int index = 0;
13         for (int i = len - 1; i > 0; i--){
14             if (Character.isSpaceChar(s.charAt(i))){
15                 index = i + 1;
16                 break;
17             }
18         }
19         return len - index;
20     }
21 }
原文地址:https://www.cnblogs.com/CuiHongYu/p/7095262.html