Java中去除字符串中空格的几种方法
2017-10-19 15:40
656 查看
2016-10-11 12:08 8434人阅读 评论(1) 收藏 举报
分类:
【Java】(3)
版权声明:本文为博主原创文章,未经博主允许不得转载。
1.直接上代码
[html] view
plain copy
package com.examplezhc.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = " Hello Word! ";
//方法1:String.trim();trim()是去掉首尾空格
System.out.println("1:"+str.trim());
//方法2:str.replaceAll(" ", ""); 去掉所有空格,包括首尾、中间
String str2 = str.replaceAll(" ", "");
System.out.println("2:"+str2);
//方法3:或者replaceAll(" +",""); 去掉所有空格,包括首尾、中间
String str3 = str.replaceAll(" +", "");
System.out.println("3:"+str3);
//方法4:、str = .replaceAll("\\s*", "");可以替换大部分空白字符, 不限于空格 ; \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个。
String str4 = str.replaceAll("\\s*", "");
System.out.println("4:"+str4);
}
}
2.打印结果:
[html] view
plain copy
10-11 11:59:43.195: I/System.out(4449): 1:Hello Word!
10-11 11:59:43.195: I/System.out(4449): 2:HelloWord!
10-11 11:59:43.195: I/System.out(4449): 3:HelloWord!
10-11 11:59:43.205: I/System.out(4449): 4:HelloWord!
Java中去除字符串中空格的几种方法
2016-10-11 12:08 8434人阅读 评论(1) 收藏 举报分类:
【Java】(3)
版权声明:本文为博主原创文章,未经博主允许不得转载。
1.直接上代码
[html] view
plain copy
package com.examplezhc.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = " Hello Word! ";
//方法1:String.trim();trim()是去掉首尾空格
System.out.println("1:"+str.trim());
//方法2:str.replaceAll(" ", ""); 去掉所有空格,包括首尾、中间
String str2 = str.replaceAll(" ", "");
System.out.println("2:"+str2);
//方法3:或者replaceAll(" +",""); 去掉所有空格,包括首尾、中间
String str3 = str.replaceAll(" +", "");
System.out.println("3:"+str3);
//方法4:、str = .replaceAll("\\s*", "");可以替换大部分空白字符, 不限于空格 ; \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个。
String str4 = str.replaceAll("\\s*", "");
System.out.println("4:"+str4);
}
}
2.打印结果:
[html] view
plain copy
10-11 11:59:43.195: I/System.out(4449): 1:Hello Word!
10-11 11:59:43.195: I/System.out(4449): 2:HelloWord!
10-11 11:59:43.195: I/System.out(4449): 3:HelloWord!
10-11 11:59:43.205: I/System.out(4449): 4:HelloWord!
相关文章推荐
- java_去除字符串中的空格的几种方法
- Java中去除字符串中所有空格的几种方法
- Java中去除字符串中所有空格的几种方法
- Java中去除字符串中空格的几种方法
- Java中去除字符串中所有空格的几种方法
- Java:去除字符串中空格的几种方法
- Java:去除字符串中空格的几种方法
- String空格删除和java删除字符串最后一个字符的几种方法
- C#去除字符串空格的几种方法
- [Java]去掉字符串中空格的几种方法
- java 中几种去除空格的方法
- C#去除字符串空格的几种方法
- C#去除字符串空格的几种方法收藏
- Java字符串中去掉空格的几种方法
- C#去除字符串空格的几种方法1.
- Java基础知识强化08:将字符串倒序输出(包括空格)的几种方法
- Java 字符串去掉空格的几种方法
- java 中去除字符串中的空格,两种方法
- C#去除字符串空格的几种方法【转载】
- Java字符串空格处理(无论有几个空格,都输出为1个空格,去除字符串前后的全角空格)