Open top menu

How to Convert Upper Case Every First Letter of word in string in Android ?

We will explain two way.

First.

the method used only 3 words.
.................................................................................................................................................................


String sentence="you are good";
String  ls = Character.toString(sString.charAt(0)).toUpperCase()+sString.substring(1);
String[] splitvalue=ls.split(" ");
String s1=splitvalue[0];
String s2=splitvalue[1];
String  s = Character.toString(s2.charAt(0)).toUpperCase()+s2.substring(1);
if(splitvalue.length==3){
    String s3=splitvalue[2];
    String  ss = Character.toString(s3.charAt(0)).toUpperCase()+s3.substring(1);
      countdetails=s1+" "+s+" "+ss;
}else {
    countdetails = s1 + " " + s;
}

...................................................................................................................................................................

Second

The best way is second method.
...................................................................................................................................................................
String str1 = MainActivity.ButtonName.toLowerCase();
final StringBuilder result = new StringBuilder(str1.length());
String[] words = str1.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));

}
countdetails=result.toString();
Tagged

0 comments