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());
上面代码的输出是:
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
方法拆分以打破线条:
for (int i = 0; i < introduction.length; i++) System.out.println(introduction[i]);
使用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
但是它们在一起它不起作用!甚至可以使用Split方法将StringBuilder组合在一起吗?
不要拆分String
之前,先拆分转换.StringBuilder
将为您执行就地字符替换并获取替换的String 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); }
输出:
W*lc*m*! J*v* F*rst S*m*st*r 455, j*v* str**t C*ty (St*t*) C**ntry 575757