2

我有一个String str = "a_bcde_fghij_k".

我想把它改成"aBcdeFghijK"

如果有一个_字符,下一个字符将变为大写并删除_字符。

我怎样才能做到这一点?

4

6 回答 6

10

I suspect you'll need to just go through this character by character, building up the string as you go. For example:

public static String underscoreToCapital(String text) {
    // This will be a bit bigger than necessary, but that shouldn't matter.
    StringBuilder builder = new StringBuilder(text.length());
    boolean capitalizeNext = false;
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (c == '_') {
            capitalizeNext = true;
        } else {
            builder.append(capitalizeNext ? Character.toUpperCase(c) : c);
            capitalizeNext = false;
        }
    }
    return builder.toString();
}
于 2013-07-16T08:40:07.427 回答
4

Regular expressions alone can't do that (there is no "touppercase" operator, so to speak).

But Guava has a nice little utility called CaseFormat that can help you:

String result = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str)

This works, even 'though your input is not strictly in UPPER_UNDERSCORE format, but CaseFormat is lenient this way (if you want the first character to be capitalized as well use UPPER_CAMEL instead).

Alternatively, if you absolutely want to use regular expressions, you can use Matcher.appendReplacement (it has a nice example in the JavaDoc):

public static final Pattern UNDERSCORE_FOLLOWED_BY_ANYTHING = Pattern
        .compile("_(.)");

public static String toUpperAfterUnderscore(String input) {
    Matcher m = UNDERSCORE_FOLLOWED_BY_ANYTHING.matcher(input);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase());
    }
    m.appendTail(sb);
    return sb.toString();
}
于 2013-07-16T08:40:55.287 回答
2

您也可以尝试拆分。

String str = "a_bcde_fghij_k"
String result[] = str.split("_");
String newstr = result[0];
for (int i=1;i<result.length;i++) {
     char first = Character.toUpperCase(result[i].charAt(0));
     newstr = newstr + first + result[i].substring(1);
}
System.out.println(newstr);

split() 采用正则表达式,如果您觉得这很重要。

于 2013-07-16T08:49:51.903 回答
1

与我略有不同的方法,但效果很好..

  String str = "a_bcde_fghij_k";
  int count=0;
  String[] splitString = (str.split("_"));

  for (String string : splitString)
  {
    count++;        
    if(count>1)
    {
        char c= string.charAt(0);
        System.out.print(string.replace(c, Character.toUpperCase(c)));
    }
    else
        System.out.print(string);
  }

不是吗?

于 2013-07-16T11:49:39.197 回答
0

您可能必须检查此方法的执行情况,但这可能是另一个想法:

public String replaceAndUpper(String word) {
    int charToRemove = word.indexOf("_");
    while (charToRemove != -1) {
        String part1 = word.substring(0, charToRemove);
        String part2 = word.substring(charToRemove + 1);
        char upperChar = Character.toUpperCase(part2.charAt(0));
        word = part1 + String.valueOf(upperChar) + part2.substring(1);
        charToRemove = word.indexOf("_");
    }
    return word;
}
于 2013-07-16T08:57:40.883 回答
0

使用正则表达式没有直接的方法,但我认为使用正则表达式可以大大简化任务:

public static String underscoreToCapital(final String input) {
    String ret = input;
    final Matcher m = Pattern.compile("_([a-z])").matcher(input);
    while (m.find()) {
        final String found = m.group();
        final String toUppercase = m.group(1);
        ret = ret.replaceAll(found, toUppercase.toUpperCase());
    }
    return ret;
}
于 2013-07-16T08:51:02.200 回答