Java中是否内置了一个函数,使字符串中每个单词的第一个字符大写,而不影响其他字符?

例子:

乔恩·斯基特->乔恩·斯基特 miles o'Brien -> miles o'Brien (B仍然是大写,这排除了Title Case) 老麦当劳->

*(老麦当劳也会被发现,但我不指望它有那么聪明。)

快速浏览一下Java字符串文档就会发现只有toUpperCase()和toLowerCase(),它们当然没有提供所需的行为。当然,谷歌的结果主要是这两个函数。它看起来像一个已经被发明出来的轮子,所以问一下也无妨,这样我以后就可以用它了。


当前回答

我做了这个小类,可以用来大写一个句子中的每个单词。如果不是空格,可以更改字符串中的单词分隔符。

package com.ecnews.ecnews_v01.Helpers;

  public class Capitalize {

  String sentence;
  String separator = " ";

  public Capitalize(String sentence) {
    this.sentence = sentence;
  }

  public void setSeparator(String separator) {
    this.separator = separator;
  }

  public String getCapitalized() {
    StringBuilder capitalized = new StringBuilder("");
    for (String word : sentence.split(separator)) {
        capitalized.append(separator+Character.toUpperCase(word.charAt(0)) + word.substring(1));
    }
    return capitalized.toString().trim();
  }

}

例子:

String sourceName = new Capitalize("this is a test"). get资本化();

sourceName将是“This Is A Test”

其他回答

试试这个非常简单的方法

示例givenString="ram是好孩子"

public static String toTitleCase(String givenString) {
    String[] arr = givenString.split(" ");
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < arr.length; i++) {
        sb.append(Character.toUpperCase(arr[i].charAt(0)))
            .append(arr[i].substring(1)).append(" ");
    }          
    return sb.toString().trim();
}  

输出将是:公羊是好孩子

Simple answer by program:


public class StringCamelCase {
    public static void main(String[] args) {
        String[] articles = {"the ", "a ", "one ", "some ", "any "};
        String[] result = new String[articles.length];
        int i = 0;
        for (String string : articles) {
            result[i++] = toUpercaseForstChar(string);
        }

        for (String string : result) {
            System.out.println(string);
        }
    }
    public static String toUpercaseForstChar(String string){
        return new String(new char[]{string.charAt(0)}).toUpperCase() + string.substring(1,string.length());
    }
}
String toBeCapped = "i want this sentence capitalized";

String[] tokens = toBeCapped.split("\\s");
toBeCapped = "";

for(int i = 0; i < tokens.length; i++){
    char capLetter = Character.toUpperCase(tokens[i].charAt(0));
    toBeCapped +=  " " + capLetter + tokens[i].substring(1);
}
toBeCapped = toBeCapped.trim();
public static String toTitleCase(String word){
    return Character.toUpperCase(word.charAt(0)) + word.substring(1);
}

public static void main(String[] args){
    String phrase = "this is to be title cased";
    String[] splitPhrase = phrase.split(" ");
    String result = "";

    for(String word: splitPhrase){
        result += toTitleCase(word) + " ";
    }
    System.out.println(result.trim());
}

intiCap的可重用方法:

    public class YarlagaddaSireeshTest{

    public static void main(String[] args) {
        String FinalStringIs = "";
        String testNames = "sireesh yarlagadda test";
        String[] name = testNames.split("\\s");

        for(String nameIs :name){
            FinalStringIs += getIntiCapString(nameIs) + ",";
        }
        System.out.println("Final Result "+ FinalStringIs);
    }

    public static String getIntiCapString(String param) {
        if(param != null && param.length()>0){          
            char[] charArray = param.toCharArray(); 
            charArray[0] = Character.toUpperCase(charArray[0]); 
            return new String(charArray); 
        }
        else {
            return "";
        }
    }
}