Java 在字符串中放置一个空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19428063/
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-08-12 17:03:59  来源:igfitidea点击:

Putting a space in a string

javastring

提问by Emmanuel

Assuming I have a String like "MikeHymanson" I am trying to figure out a way to put a space in between so it becomes "Mike Hymanson". And then applying the same method to another string say "JohnBull" would give me back "John Bull". This is the code I came up with:

假设我有一个像“MikeHymanson”这样的字符串,我正试图找出一种在两者之间放置一个空格的方法,这样它就变成了“Mike Hymanson”。然后对另一个字符串应用相同的方法说“JohnBull”会给我回“John Bull”。这是我想出的代码:

public class Test{

    public Test(){

    }
public void sep(String s){
    s = s + " ";
   char[] charArray = s.toCharArray();
   int l = s.length();
for (int i = 0; i < l; i++){
    char p = ' ';

    if(Character.isUpperCase(s.charAt(0))){
        continue;   
    }
    else if (Character.isUpperCase(s.charAt(i))){
        int k = s.indexOf(s.charAt(i));
        charArray[l] = charArray[--l];
        charArray[k-1] = p;
    }
    //System.out.println(s.charAt(i));
}
}
    public static void main (String args[]){

    Test one = new Test();

    one.sep("MikeHymanson");
    }  
}

My idea was to add a space to the String so that "MikeHymanson" becomes "Mike Hymanson " and then shift the characters on place to the right (check for where I find an uppercase) ignoring the first uppercase. Then put a character ' ' in place of the character 'J' but shift 'J' to the right. That's what I was trying to achieve with my method but it looks I need some guidelines. If anyone could help. Thanks.

我的想法是在字符串中添加一个空格,以便“MikeHymanson”变成“Mike Hymanson”,然后将字符向右移动(检查我找到大写的位置)忽略第一个大写。然后将字符 ' ' 放在字符 'J' 的位置,但将 'J' 向右移动。这就是我试图用我的方法实现的目标,但看起来我需要一些指导方针。如果有人可以帮忙。谢谢。

采纳答案by Silviu Burcea

Try this:

尝试这个:

"MikeHymanson".replaceAll("(?!^)([A-Z])", " ");

For every upper char I am adding a space before.

对于每个上层字符,我之前都添加了一个空格。

Also, it works with multiple uppercase words. I am getting Word1 Word2 Word3for Word1Word2Word3.

此外,它适用于多个大写单词。我得到Word1 Word2 Word3Word1Word2Word3

回答by RamonBoza

String is finaland immutable, you cannot modify it, you will always use it to create a new one and assign to any variable.

字符串是finaland immutable,您不能修改它,您将始终使用它来创建一个新的并分配给任何变量。

Being that said, i would recommend to look for the first non-zero index uppercase, get the substring where it is located, store the two substrings and add and space between.

话虽如此,我建议查找第一个非零索引大写字母,获取它所在的子字符串,存储两个子字符串并在它们之间添加和空格。

回答by X-Pippes

Similar question here: Insert Space After Capital letter

类似问题: 在大写字母后插入空格

try it and if you have any questions let us know!

试试吧,如果您有任何问题,请告诉我们!

the code from reference is here:

参考代码在这里:

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

回答by Jeroen Vannevel

public static void sep(String s) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {

        result.append(s.charAt(i));
        if (i != s.length() -1 && Character.isUpperCase(s.charAt(i + 1))) {
            result.append(" ");
        }
    }
    System.out.println(result);
}

Simply add a space if the next character is uppercase.

如果下一个字符是大写,只需添加一个空格。

回答by npinti

The easiest way to go round this, in this case would be to use regular expressions

在这种情况下,最简单的方法是使用正则表达式

    String str = "MikeHymanson";
    System.out.println(str.replaceAll("(\w+?)([A-Z])(\w+?)", " "));

Yields: Mike Hymanson

产量: Mike Hymanson

回答by devnull

Using String.replaceAll:

使用String.replaceAll

String foo = "SomeLongName";
System.out.println(foo.replaceAll("([a-z]+)([A-Z])", " "));

Results in Some Long Name.

结果在Some Long Name.

回答by ferrerverck

public static String addSpaces(String str) {
    StringBuilder sb = new StringBuilder();
    if (str.length() == 0) return "";
    sb.append(str.charAt(0));
    for (int i = 1; i < str.length(); i++) {
        if (Character.isUpperCase(str.charAt(i))) sb.append(" ");
        sb.append(str.charAt(i));
    }
    return sb.toString();
}