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
How to use split method with stringbuilder together in the same class?
提问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 String
before, first to the transformation. StringBuilder
will 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 给出的答案。我不是专家,但这些是我的观察:
- There is no need to use StringBuilder. Read more here: when to use StringBuilder in java
- You can use regex for transformation. Read more here: How to replace replace vowels with a special character in Java?
- 无需使用 StringBuilder。在此处阅读更多信息:何时在 Java 中使用 StringBuilder
- 您可以使用正则表达式进行转换。在此处阅读更多信息:如何在 Java 中用特殊字符替换替换元音?
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"));