1

I need to split a string by capitals and preferably make subsequent words lower case;

for example

ThisIsAString becomes This is a string

Thanks

Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97
  • 1
    have you copied question from http://stackoverflow.com/questions/3752636/java-split-string-when-an-uppercase-letter-is-found – Jama A. Feb 10 '12 at 16:49
  • 2
    "give me teh codez" gets an up-vote? – skaffman Feb 10 '12 at 16:49
  • 1
    possible duplicate of [How do I convert CamelCase into human-readable names in Java?](http://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-java) – Andrew Feb 10 '12 at 17:04
  • No this is not a duplicate of anything thanks – Ben Taliadoros Feb 12 '12 at 03:13
  • Does this answer your question? [How do I convert CamelCase into human-readable names in Java?](https://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-java) – Pino May 20 '21 at 08:02

4 Answers4

4

If you want a string you can do so:

String str = "ThisIsAString";
String res = "";
for(int i = 0; i < str.length(); i++) {
   Character ch = str.charAt(i);
     if(Character.isUpperCase(ch))
       res += " " + Character.toLowerCase(ch);
     else
       res += ch;
}
Marvin
  • 853
  • 2
  • 14
  • 38
Figus
  • 681
  • 1
  • 7
  • 13
3

It looks like you want to convert camelcase into readable language. Is that the case?

If so, this solution should work for you - How do I convert CamelCase into human-readable names in Java?

If you want subsequent words lowercased, you'll have to split to handle that yourself.

Community
  • 1
  • 1
I82Much
  • 26,901
  • 13
  • 88
  • 119
1
public static String cpt(String cpt){
    String new_string = "";
    for (int i=0; i < cpt.length(); i++){
        char c = cpt.charAt(i);
        if(Character.isUpperCase(c)){               
            new_string = new_string + " " + Character.toLowerCase(c);
        }
        else {
            new_string = new_string + c;
        }
    }
    return new_string;
}
bernabas
  • 481
  • 1
  • 4
  • 12
0

Total Number of Words in a Camel Case word using char casing identification

Solution: to Hacker Rank camel case problem: https://www.hackerrank.com/challenges/camelcase/problem

public static void camelCaseWordWithIndexs(String s) {


    int startIndex = 0;
    int endIndex = 0;

    for (int i = 0; i < s.length(); i++) {
        if (Character.isUpperCase(s.charAt(i)) || i == s.length() - 1) {
            startIndex = endIndex;
            endIndex = i != s.length() - 1 ? i : i + 1;
            System.out.println(s.substring(startIndex, endIndex));
        }
    }


}
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50