当前位置:  开发笔记 > 编程语言 > 正文

如何在同一个类中将splitbuilder与splitbuilder一起使用?

如何解决《如何在同一个类中将splitbuilder与splitbuilder一起使用?》经验,为你挑选了1个好方法。

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组合在一起吗?



1> YoungHobbit..:

不要拆分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 

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有