0

I was wondering if someone could provide me some code or point me towards a tutrial which explain how I can convert my string so that each word begins with a capital.

I would also like to convert a different string in italics.

Basically, what my app is doing is getting data from several EditText boxes and then on a button click is being pushed onto the next page via intent and being concatenated into 1 paragraph. Therefore, I assume I need to edit my string on the intial page and make sure it is passed through in the same format.

Thanks in advance

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • You might want to check this [question](http://stackoverflow.com/questions/1149855/how-to-upper-case-every-first-letter-of-word-in-a-string) as it is similar to yours – Adel Boutros Jan 17 '12 at 16:46
  • The 'convert to italics' part is a totally different thing. One thing is a string (a sequence of chars abstracted from any format) and other is formatted-text. Probably you want to modify font properties of some UI widgets to show its text in italics. – helios Jan 17 '12 at 16:50

6 Answers6

1

Here is a simple function

public static String capEachWord(String source){
        String result = "";
        String[] splitString = source.split(" ");
        for(String target : splitString){
            result
                    += Character.toUpperCase(target.charAt(0))
                    + target.substring(1) + " ";
        }
        return result.trim();
    }
1

You can use Apache StringUtils. The capitalize method will do the work.

For eg:

WordUtils.capitalize("i am FINE") = "I Am FINE"

or

WordUtils.capitalizeFully("i am FINE") = "I Am Fine"
RanRag
  • 48,359
  • 38
  • 114
  • 167
0
String source = "hello good old world";
StringBuilder res = new StringBuilder();

String[] strArr = source.split(" ");
for (String str : strArr) {
    char[] stringArray = str.trim().toCharArray();
    stringArray[0] = Character.toUpperCase(stringArray[0]);
    str = new String(stringArray);

    res.append(str).append(" ");
}

System.out.print("Result: " + res.toString().trim());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
duggu
  • 37,851
  • 12
  • 116
  • 113
0

The easiest way to do this is using simple Java built-in functions.

Try something like the following (method names may not be exactly right, doing it off the top of my head):

String label = Capitalize("this is my test string");    


public String Capitalize(String testString)
{
    String[] brokenString = testString.split(" ");
    String newString = "";

    for(String s : brokenString)
    {
        s.charAt(0) = s.charAt(0).toUpper();
        newString += s + " ";
    }

    return newString;
}

Give this a try, let me know if it works for you.

Codeman
  • 12,157
  • 10
  • 53
  • 91
0

Just add
android:inputType="textCapWords" to your EditText in layout xml.
This wll make all the words start with the Caps letter.

akkilis
  • 1,904
  • 14
  • 21
0

Strings are immutable in Java, and String.charAt returns a value, not a reference that you can set (like in C++). Pheonixblade9's will not compile. This does what Pheonixblade9 suggests, except it compiles.

public String capitalize(String testString) {
    String[] brokenString = testString.split(" ");
    String newString = "";

    for (String s : brokenString) {
        char[] chars = s.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);
        newString = newString + new String(chars) + " ";
    }

    //the trim removes trailing whitespace
    return newString.trim();
}
Ned Twigg
  • 2,159
  • 2
  • 22
  • 38