How to capitalize the first character of each word in a string

后端 未结 30 1335
情深已故
情深已故 2020-11-22 02:08

Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?

Examples:

  • jon
相关标签:
30条回答
  • 2020-11-22 02:33

    Reusable method for 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 "";
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:35
    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();
    
    0 讨论(0)
  • 2020-11-22 02:36

    For those of you using Velocity in your MVC, you can use the capitalizeFirstLetter() method from the StringUtils class.

    0 讨论(0)
  • 2020-11-22 02:37

    If you're only worried about the first letter of the first word being capitalized:

    private String capitalize(final String line) {
       return Character.toUpperCase(line.charAt(0)) + line.substring(1);
    }
    
    0 讨论(0)
  • 2020-11-22 02:37

    I made a solution in Java 8 that is IMHO more readable.

    public String firstLetterCapitalWithSingleSpace(final String words) {
        return Stream.of(words.trim().split("\\s"))
        .filter(word -> word.length() > 0)
        .map(word -> word.substring(0, 1).toUpperCase() + word.substring(1))
        .collect(Collectors.joining(" "));
    }
    

    The Gist for this solution can be found here: https://gist.github.com/Hylke1982/166a792313c5e2df9d31

    0 讨论(0)
  • 2020-11-22 02:38
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
    
    System.out.println("Enter the sentence : ");
    
    try
    {
        String str = br.readLine();
        char[] str1 = new char[str.length()];
    
        for(int i=0; i<str.length(); i++)
        {
            str1[i] = Character.toLowerCase(str.charAt(i));
        }
    
        str1[0] = Character.toUpperCase(str1[0]);
        for(int i=0;i<str.length();i++)
        {
            if(str1[i] == ' ')
            {                   
                str1[i+1] =  Character.toUpperCase(str1[i+1]);
            }
            System.out.print(str1[i]);
        }
    }
    catch(Exception e)
    {
        System.err.println("Error: " + e.getMessage());
    }
    
    0 讨论(0)
提交回复
热议问题