0

I have a String str which can have list of values like below. I want the first letter in the string to be uppercase and if underscore appears in the string then i need to remove it and need to make the letter after it as upper case. The rest all letter i want it to be lower case.

""
"abc" 
"abc_def"
"Abc_def_Ghi12_abd"
"abc__de"
"_"
Output:
""
"Abc"
"AbcDef"
"AbcDefGhi12Abd"
"AbcDe"
""
Joey
  • 344,408
  • 85
  • 689
  • 683
Arav
  • 4,957
  • 23
  • 77
  • 123
  • 1
    Wow, that's a nice idea ... Hope you'll succeed without asking one of those beginners questions like "where is the doc for the String class saying all i could want about the methods I need to do my homework". – Riduidel Mar 15 '10 at 10:17
  • So you have *one* string but apparently *four* individual “first” letters in that string? – Joey Mar 15 '10 at 10:20
  • This is similar to what Commons Lang WordUtils.capitalize does. – Thilo Mar 15 '10 at 10:23
  • 2
    Show what you have tried so far. – MAK Mar 15 '10 at 10:26
  • Actually, it's very similar to another question of that user too: http://stackoverflow.com/questions/2375649/converting-to-upper-and-lower-case-in-java – Joey Mar 15 '10 at 10:26
  • I have one String which can one of the above listed values – Arav Mar 15 '10 at 10:32

4 Answers4

1

Well, without showing us that you put any effort into this problem this is going to be kinda vague.

I see two possibilities here:

  1. Split the string at underscores, apply the answer from this question to each part and re-combine them.
  2. Create a StringBuilder, walk through the string and keep track of whether you are

    • at the start of the string
    • after an underscore or
    • somewhere else

    and act appropriately on the current character before appending it to the StringBuilder instance.

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683
1
  1. replace _ with space (str.replace("_", " "))
  2. use WordUtils.capitalizeFully(str); (from commons-lang)
  3. replace space with nothing (str.replace(" ", ""))
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

You can use following regexp based code:

public static String camelize(String input) {
  char[] c = input.toCharArray();
  Pattern pattern = Pattern.compile(".*_([a-z]).*");
  Matcher m = pattern.matcher(input);
  while ( m.find() ) {
    int index = m.start(1);
    c[index] = String.valueOf(c[index]).toUpperCase().charAt(0); 
  }
  return String.valueOf(c).replace("_", "");
}
Denis Bazhenov
  • 9,680
  • 8
  • 43
  • 65
0

Use Pattern/Matcher in the java.util.regex package:

for each string that is in your array do the following:

StringBuffer output = new StringBuffer();
Matcher match = Pattern.compile("[^|_](\w)").matcher(inStr);
while(match.find()) {
   match.appendReplacement(output, matcher.match(0).ToUpper());
}
match.appendTail(output);

// Will have the properly capitalized string.
String capitalized = output.ToString();

The regular expression looks for either the start of the string or an underscore "[^|_]" Then puts the following character into a group "(\w)"

The code then goes through each of the matches in the input string capitalizing the first satisfying group.

Adrian Regan
  • 2,240
  • 13
  • 11