/*
* \s 空格(\u0008)
* \n 回车(\u000a)
* \r 换行(\u000d)
* 1.去除空格:newText = Text.replace('\\s','');
* 2.去除回车:newText = Text.replace('\n','');
*/
public static void main(String[] args){
String Text = "万 达 广 场 市 内 BTS 掉 站";
String newText = Text.replaceAll("\\s", "");
System.out.println(newText);
}
}
public static void main(String[] args){
String Text = "万 达 广 场 市 内 BTS 掉 站";
StringBuilder strBuilder = new StringBuilder(); //创建字符串构造器
for(int i =0; i < Text.length(); i++){ //遍历字符串
char charAt = Text.charAt(i); //获取每个字符
if(!(charAt==' ')){ //过滤空格字符串
String str= String.valueOf(charAt); //String.valueOf(char[])方法将参数转换为字符串
strBuilder.append(str);
}
}
System.out.println(strBuilder);
}
}
*/
* \s 空格(\u0008)
* \n 回车(\u000a)
* \r 换行(\u000d)
* 1.去除空格:newText = Text.replace('\\s','');
* 2.去除回车:newText = Text.replace('\n','');
*/
//第一种方法
public static void main(String[] args){
String Text = "万 达 广 场 市 内 BTS 掉 站";
String newText = Text.replaceAll("\\s", "");
System.out.println(newText);
}
}
//第二种方法
/*
public static void main(String[] args){
String Text = "万 达 广 场 市 内 BTS 掉 站";
StringBuilder strBuilder = new StringBuilder(); //创建字符串构造器
for(int i =0; i < Text.length(); i++){ //遍历字符串
char charAt = Text.charAt(i); //获取每个字符
if(!(charAt==' ')){ //过滤空格字符串
String str= String.valueOf(charAt); //String.valueOf(char[])方法将参数转换为字符串
strBuilder.append(str);
}
}
System.out.println(strBuilder);
}
}
*/