java 在大写字母后插入空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4886091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:28:21  来源:igfitidea点击:

Insert Space After Capital letter

javastring

提问by Emil

How to covert "HelloWorld"to "Hello World".The splitting has to take place based on The Upper-Case letters ,but should exclude the first letter.

如何转换"HelloWorld""Hello World". 拆分必须基于大写字母进行,但应排除第一个字母。

P.S:I'm aware of using String.split and then combining.Just wanted to know if there is a better way.

PS:我知道使用 String.split 然后组合。只是想知道是否有更好的方法。

回答by Joachim Sauer

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

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 ($1and $2).

此正则表达式搜索后跟大写字母的小写字母,并将它们替换为前者、空格和后者(有效地用空格分隔它们)。它将它们中的每个放在一个捕获组()中,以便能够通过反向引用($1$2)重新使用替换字符串中的值。

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

要查找它使用的大写和小写字母\p{Ll}\p{Lu}(而不是[a-z][A-Z]),因为它处理Unicode 标准中的所有大写和小写字母,而不仅仅是 ASCII 范围内的字母(正则表达式中对 Unicode 的这个很好的解释主要适用于 Java同样)。

回答by Andreas Dolk

Betteris 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 ;) )

将这个丑陋的实现隐藏在一个实用程序类中并将其用作 API(从用户的角度来看似乎没问题;))

回答by Nishant

    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

你好世界尼桑特

回答by Michael Berry

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);
}

回答by vz0

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;
}