将标题首字母大写 (Capitalize the Title)

 

思路:

// @Title: 将标题首字母大写 (Capitalize the Title)
// @Author: qisiii
// @Date: 2024-03-11 10:53:54
// @Runtime: 3 ms
// @Memory: 41.6 MB
// @comment: 
// @flag: 
class Solution {
    public String capitalizeTitle(String title) {
        String[] strs = title.split(" ");
        StringBuilder strb = new StringBuilder();
        for (String s : strs) {
            s = s.toLowerCase();
            if (s.length() > 2) {
                char c = s.charAt(0);
                strb.append(String.valueOf(c).toUpperCase());
                strb.append(s.substring(1));
            }else{
                strb.append(s);
            }
            strb.append(" ");
        }
        return strb.toString().trim();
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18