当前位置 : 主页 > 编程语言 > java >

java108-StringBuilder连接字符串和删除操作

来源:互联网 收集:自由互联 发布时间:2022-07-04
public class test48 { //连接字符串 public static String concatString ( String base , String [] str ){ StringBuilder builder = new StringBuilder ( base ); //构建可变字符串 for ( String s : str ){ builder . append ( " " + s ); } r
public class test48 {
//连接字符串
public static String concatString(String base,String[] str){
StringBuilder builder=new StringBuilder(base);//构建可变字符串
for(String s:str){
builder.append(" "+s);
}
return builder.toString();
}
//删除空格
public static String deleteSpace(String target){
while(target.contains(" ")){
StringBuilder builder=new StringBuilder(target);
int index=builder.indexOf(" ");
builder.deleteCharAt(index);
target=builder.toString();
}
return target;
}
}


测试类

//stringbuilder
public class test49 {
public static void main(String[] args){
StringBuilder builder=new StringBuilder("我是一个热血的少年");
//System.out.println(builder);
String str="今天会下雨吗";
String[] strs={"会的","也许会的","谁知道呢"};
String str1=test48.concatString(str,strs);
System.out.println(str1);
String str3="我 是 歌谣";
//test48.deleteSpace(str3);
String str4=test48.deleteSpace(str3);
System.out.println(str4);
}
}


 


网友评论