22

How to convert "HelloWorld" to "Hello World"? The splitting has to take place based on The upper-case letters, but should exclude the first letter.

cigien
  • 57,834
  • 11
  • 73
  • 112
Emil
  • 13,577
  • 18
  • 69
  • 108
  • Posting as a comment rather than answer because I don't know the syntax. But I'm sure it would be very easy to write a regular expression that would look for a capital letter (that is not at the beginning of the sentence) and add a space before it. – DaveJohnston Feb 03 '11 at 12:27
  • 1
    Do you expect `convert("HELLO") == "H E L L O"` ? – vz0 Feb 03 '11 at 12:27
  • @vz0:Actually my string's will have some lower case in between the upper cases. – Emil Feb 03 '11 at 12:29

5 Answers5

64
String output = input.replaceAll("(\\p{Ll})(\\p{Lu})","$1 $2");

This regex searches for a lowercase letter follwed by an uppercase letter and replaces them with the former, a space and the latter (effectively separating them with a space). It puts each of them in a capturing group () in order to be able to re-use the values in the replacement string via back references ($1 and $2).

To find upper- and lowercase letters it uses \p{Ll} and \p{Lu} (instead of [a-z] and [A-Z]), because it handles all upper- and lowercase letters in the Unicode standard and not just the ones in the ASCII range (this nice explanation of Unicode in regexes mostly applies to Java as well).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
5

Better is subjective. This takes some more lines of code:

public static String deCamelCasealize(String camelCasedString) {
    if (camelCasedString == null || camelCasedString.isEmpty())
        return camelCasedString;

    StringBuilder result = new StringBuilder();
    result.append(camelCasedString.charAt(0));
    for (int i = 1; i < camelCasedString.length(); i++) {
        if (Character.isUpperCase(camelCasedString.charAt(i)))
        result.append(" ");
        result.append(camelCasedString.charAt(i));
    }
    return result.toString();
}

Hide this ugly implementation in a utility class and use it as an API (looks OK from the user perspective ;) )

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • +1 its a good implementation but i prefer regex.I just have to covert a few strings. – Emil Feb 03 '11 at 12:42
3
    String s = "HelloWorldNishant";
    StringBuilder out = new StringBuilder(s);
    Pattern p = Pattern.compile("[A-Z]");
    Matcher m = p.matcher(s);
    int extraFeed = 0;
    while(m.find()){
        if(m.start()!=0){
            out = out.insert(m.start()+extraFeed, " ");
            extraFeed++;
        }
    }
    System.out.println(out);

prints

Hello World Nishant

Nishant
  • 54,584
  • 13
  • 112
  • 127
1

If you don't want to use regular expressions you could loop through the characters in the string, adding them to a stringbuilder (and adding a space to the string builder if you come across a capital letter that's not the first):

String s = "HelloWorld";
StringBuilder result = new StringBuilder();
for(int i=0 ; i<s.length() ; i++) {
    char c = s.charAt(i);
    if(i!=0&&Character.isUpperCase(c)) {
        result.append(' ');
    }
    result.append(c);
}
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

Pseudocode:

String source = ...;
String result = "";

// FIXME: check for enf-of-source

for each letter in source {
    while current letter not uppercase {
        push the letter to result;
        advance one letter;
    }
    if not the first letter {
        push space to result;
    }
    push the letter to result;
}
vz0
  • 32,345
  • 7
  • 44
  • 77