Advertisement
Advertisement


How to upper case every first letter of word in a string?


Question

I have a string: "hello good old world" and i want to upper case every first letter of every word, not the whole string with .toUpperCase(). Is there an existing java helper which does the job?

2020/02/02
1
93
2/2/2020 1:35:00 PM

Accepted Answer

Have a look at ACL WordUtils.

WordUtils.capitalize("your string") == "Your String"
2015/07/15
128
7/15/2015 3:26:22 PM


sString = sString.toLowerCase();
sString = Character.toString(sString.charAt(0)).toUpperCase()+sString.substring(1);
2012/08/10

i dont know if there is a function but this would do the job in case there is no exsiting one:

String s = "here are a bunch of words";

final StringBuilder result = new StringBuilder(s.length());
String[] words = s.split("\\s");
for(int i=0,l=words.length;i<l;++i) {
  if(i>0) result.append(" ");      
  result.append(Character.toUpperCase(words[i].charAt(0)))
        .append(words[i].substring(1));

}
2009/07/19

import org.apache.commons.lang.WordUtils;

public class CapitalizeFirstLetterInString {
    public static void main(String[] args) {
        // only the first letter of each word is capitalized.
        String wordStr = WordUtils.capitalize("this is first WORD capital test.");
        //Capitalize method capitalizes only first character of a String
        System.out.println("wordStr= " + wordStr);

        wordStr = WordUtils.capitalizeFully("this is first WORD capital test.");
        // This method capitalizes first character of a String and make rest of the characters lowercase
        System.out.println("wordStr = " + wordStr );
    }
}

Output :

This Is First WORD Capital Test.

This Is First Word Capital Test.

2012/01/31

Here's a very simple, compact solution. str contains the variable of whatever you want to do the upper case on.

StringBuilder b = new StringBuilder(str);
int i = 0;
do {
  b.replace(i, i + 1, b.substring(i,i + 1).toUpperCase());
  i =  b.indexOf(" ", i) + 1;
} while (i > 0 && i < b.length());

System.out.println(b.toString());

It's best to work with StringBuilder because String is immutable and it's inefficient to generate new strings for each word.

2011/03/30

Trying to be more memory efficient than splitting the string into multiple strings, and using the strategy shown by Darshana Sri Lanka. Also, handles all white space between words, not just the " " character.

public static String UppercaseFirstLetters(String str) 
{
    boolean prevWasWhiteSp = true;
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if (Character.isLetter(chars[i])) {
            if (prevWasWhiteSp) {
                chars[i] = Character.toUpperCase(chars[i]);    
            }
            prevWasWhiteSp = false;
        } else {
            prevWasWhiteSp = Character.isWhitespace(chars[i]);
        }
    }
    return new String(chars);
}
2011/09/09

Source: https://stackoverflow.com/questions/1149855
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]