Java 如何在同一个类中将 split 方法与 stringbuilder 一起使用?

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

How to use split method with stringbuilder together in the same class?

javasplitstringbuilder

提问by Jane Nice

StringBuilder to hide vowels:

StringBuilder 隐藏元音:

String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
StringBuilder sb = new StringBuilder(bienvenue_intro);
String[] introduction = bienvenue_intro.split(":"); 
for (int i = 0; i < bienvenue_intro.length(); i++) {
    char c = bienvenue_intro.charAt(i);
    if ((c == 'A') || (c == 'a') ||
        (c == 'E') || (c == 'e') ||
        (c == 'I') || (c == 'i') ||
        (c == 'O') || (c == 'o') ||
        (c == 'U') || (c == 'u')) {
            sb.setCharAt(i, '*');
    }
}
System.out.println(bienvenue_intro);
System.out.println(sb.toString());

The output of the above code is:

上面代码的输出是:

Welcome! Java First Semester: 455, java street: City (State): Country: 575757 
 W*lc*m*! J*v* F*rst S*m*st*r: 455, j*v* str**t: C*ty (St*t*): C**ntry: 575757 

Method split to break the lines:

方法 split 断行:

for (int i = 0; i < introduction.length; i++)
    System.out.println(introduction[i]);

The desired output using split + string builder would be:

使用 split + string builder 所需的输出是:

W*lc*m*! J*v* F*rst S*m*st*r

 455, j*v* str**t

 C*ty (St*t*)

 C**ntry

 575757 

But both together it does not work! Is it even possible to team StringBuilder with the Split method?

但两者放在一起是行不通的!甚至可以将 StringBuilder 与 Split 方法组合在一起吗?

采纳答案by YoungHobbit

Don't split the Stringbefore, first to the transformation. StringBuilderwill perform the in-place character replacement for you and get the replaced String using toString()and perform the split on it.

不分String前,先来改造。StringBuilder将为您执行就地字符替换,并使用替换的字符串toString()并对其执行拆分。

String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
StringBuilder sb = new StringBuilder(bienvenue_intro);

for (int i = 0; i < bienvenue_intro.length(); i++) {
  char c = bienvenue_intro.charAt(i);
  if (   (c == 'A') || (c == 'a')
      || (c == 'E') || (c == 'e')
      || (c == 'I') || (c == 'i')
      || (c == 'O') || (c == 'o')
      || (c == 'U') || (c == 'u')) {
    sb.setCharAt(i, '*');
  }
}
System.out.println(bienvenue_intro);
//System.out.println(sb.toString());

String[] introduction = sb.toString().split(":");  //<-- Do the split here after replacements.
for (String string : introduction) {
  System.out.println(string);
}

Output:

输出:

 W*lc*m*! J*v* F*rst S*m*st*r
 455, j*v* str**t
 C*ty (St*t*)
 C**ntry
 575757 

回答by TheLostMind

There is a better and simpler way of doing this :)

有一种更好、更简单的方法可以做到这一点:)

replaceAll()and split().

replaceAll()split()

public static void main(String[] args) {
    String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
    String[] arr = bienvenue_intro.replaceAll("(?i)[aeiou]+", "*").split(":");

    for(String s : arr) {
        System.out.println(s);
    }
}

 W*lc*m*! J*v* F*rst S*m*st*r
 455, j*v* str*t
 C*ty (St*t*)
 C*ntry
 575757 

回答by Suhas K

I would like to expand on the answer given by @YoungHobbit. I am not an expert but these are my observations:

我想扩展@YoungHobbit 给出的答案。我不是专家,但这些是我的观察:

My take:

我的看法:

String bienvenue_intro = " Welcome! Java First Semester: 455, java street: City (State): Country: 575757 ";
String mod_str = bienvenue_intro.replaceAll( "(?i)[aeiou]", "*" );
String[] introduction = mod_str.split(":");
for (String string : introduction) {
  System.out.println(string);
}

回答by Bohemian

I believe your approach is all wrong: There is already an API to do this in one, easily-readable, line of code:

我相信你的方法完全是错误的:已经有一个 API 可以在一个易于阅读的代码行中做到这一点:

System.out.println(bienvenue_intro.replaceAll("[aeiouAEIOU]", "*").replace(":", "\n"));