0

Is there a built in function to do title-case for you or will I have to write a custom function? I'm attempting to title-case a full name.

I.E. john doe should be converted to: John Doe

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
jcaruso
  • 2,364
  • 1
  • 31
  • 64
  • This is pretty simple using standard `String` libraries, just loop through the string word by word and change first char to an upper case one. – Hans Petter Taugbøl Kragset Apr 30 '14 at 20:17
  • That's not the definition of "camel case." You mean "title case." - Edited. – 323go Apr 30 '14 at 20:49
  • @323go Thank you for the correction, but the down vote was not necessary. – jcaruso Apr 30 '14 at 20:59
  • Does this answer your question? [How to capitalize the first character of each word in a string](https://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string) – M. Justin Dec 14 '22 at 19:53

3 Answers3

1

The simplest code I can figure out at the moment:

String str = "mR. jOhn smIth";
final String[] arr = TextUtils.split(str," ");
final int len = arr.length;

for(int i = 0; i < len; i++)
{
    final String s = arr[i];
    final String s0 = "" + s.toUpperCase().charAt(0);
    final String s1 =  s.toLowerCase().substring(1, s.length());
    arr[i] = s0 + s1;
}

str = TextUtils.join(" ", arr); // and now str contains "Mr. John Smith"
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

No it does'nt exist in the official Android API, but it's pretty easy to wrote your own one. Something like this:

public static String camelCasedString(String s){
    boolean shouldCapitalize = true;
    char[] arr = s.toCharArray();
    for(int i = 0; i < arr.length; i++){
        if(arr[i] == ' '){
            shouldCapitalize = true;
        } else if(shouldCapitalize){
            arr[i] = Character.toUpperCase(arr[i]);
            shouldCapitalize = false;
        } else {
            arr[i] = Character.toLowerCase(arr[i]);
        }
    }
    return new String(arr);
}

Output:

System.out.println(camelCasedString("john dOe")); //John Doe
System.out.println(camelCasedString("john doe")); //John Doe
System.out.println(camelCasedString("John Doe")); //John Doe
System.out.println(camelCasedString("JOHN DOE")); //John Doe
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 2
    While this answer works in most real world conditions, in the wild, you have to account for surnames that properly begin with lower case characters (e.g. "de Kamp," "von Haugwitz" and so on). – MarsAtomic Apr 30 '14 at 20:38
  • @MarsAtomic Yes but that's not too hard to take in account those cases. This is giving a start for the OP. – Alexis C. Apr 30 '14 at 20:44
-1

You haven't said what language so I'll just assume javascript. The most compact way I know of doing it is:

String.prototype.titleCase = function() {
    t = this.split(' ').map((word) => word.charAt(0).toUpperCase()+word.slice(1)).join(' ');
    return t;
}

Since I've declared the function on the String.prototype, you can use it thus:

"This is a title".titleCase() // >> "This Is A Title"

which may not be 100% the way newspaper editors would have it, but it does what you seem to want it to do. Let me know if you need further explanation.

PakiPat
  • 1,020
  • 14
  • 27
  • the question is tagged as [tag:java] - notice the tags under the question on the left side? You should also see it in the title bar of the tab/window... – Sᴀᴍ Onᴇᴌᴀ Jun 23 '17 at 23:09
  • I notice that now that you've pointed it out. Was the downvote really necessary? – PakiPat Jun 23 '17 at 23:42
  • Well a down vote implies _not useful_, since while a java developer could maybe use the concepts, he/she can't use the examples. if you update your answer to pertain to java then I would reverse... – Sᴀᴍ Onᴇᴌᴀ Jun 25 '17 at 13:44