java去除空格、标点符号的方法实例
作者:iCoding91 时间:2021-12-01 16:16:42
代码如下:
public class TempTest {
public static void main(String[] args) {
//string去除空格
String str=" hello world ";
System.out.println(str);
String str1=str.trim();//去除首尾空格
System.out.println(str1);
String str2=str.replace(" ","");//去掉所有空格,包括首尾,中间
System.out.println(str2);
String str3=str.replaceAll(" +","");//去掉所有空格,包括首尾,中间
System.out.println(str3);
String str4=str.replaceAll("\\s*",""); //可以替换大部分空白字符, 不限于空格 . 说明:\s 可以匹配空格、制表符、换页符等空白字符的其中任意一个
System.out.println(str4);
//string去除标点符号
//正则表达式去除标点
String stri="ss&*(,.~1如果@&(^-自己!!知道`什`么#是$苦%……Z,&那*()么一-=定——+告诉::;\"'/?.,><[]{}\\||别人什么是甜。";
System.out.println(stri);
String stri1=stri.replaceAll("\\p{Punct}","");//不能完全清除标点
System.out.println(stri1);
String stri2=stri.replaceAll("\\pP","");//完全清除标点
System.out.println(stri2);
String stri3=stri.replaceAll("\\p{P}","");//同上,一样的功能
System.out.println(stri3);
String stri4=stri.replaceAll("[\\pP\\p{Punct}]","");//清除所有符号,只留下字母 数字 汉字 共3类.
System.out.println(stri4);
}
}
运行结果:
hello world
hello world
helloworld
helloworld
helloworld
ss&*(,.~1如果@&(^-自己!!知道`什`么#是$苦%……Z,&那*()么一-=定——+告诉::;"'/?.,><[]{}\||别人什么是甜。
ss1如果自己知道什么是苦……Z,那么一定——告诉别人什么是甜。
ss~1如果^自己知道`什`么是$苦Z那么一=定+告诉><||别人什么是甜
ss~1如果^自己知道`什`么是$苦Z那么一=定+告诉><||别人什么是甜
ss1如果自己知道什么是苦Z那么一定告诉别人什么是甜
关于replace 和replaceAll:
replace(char oldChar,char newChar)
replace(CharSequence target,CharSequence replacement)
replaceAll(String regex,String replacement)
1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);
2)replaceAll的参数是regex,即基于规则表达式的替换,比如,可以通过replaceAll("\\d", "*")把一个字符串所有的数字字符都换成星号;
相同点是都是全部替换,即把源字符串中的某一字符或字符串全部换成指定的字符或字符串,如果只想替换第一次出现的,可以使用 replaceFirst(),这个方法也是基于规则表达式的替换,但与replaceAll()不同的是,只替换第一次出现的字符串;
另外,如果replaceAll()和replaceFirst()所用的参数据不是基于规则表达式的,则与replace()替换字符串的效果是一样的,即这两者也支持字符串的操作;
还有一点注意:执行了替换操作后,源字符串的内容是没有发生改变的.
来源:https://blog.csdn.net/caoxiaohong1005/article/details/71785312
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中继承thread类与实现Runnable接口的比较
![](https://img.aspxhome.com/file/2023/8/70548_0s.jpg)
Java8 Lambda表达式详解及实例
Springboot actuator应用后台监控实现
![](https://img.aspxhome.com/file/2023/1/76301_0s.png)
浅谈Spring @Async异步线程池用法总结
Spring Boot缓存实战之Redis 设置有效时间和自动刷新缓存功能(时间支持在配置文件中配置)
![](https://img.aspxhome.com/file/2023/3/59053_0s.png)
java实现猜拳游戏
Java基础教程之static五大应用场景
Mybatis是这样防止sql注入的
深入理解Java中观察者模式与委托的对比
![](https://img.aspxhome.com/file/2023/2/58712_0s.png)
WebView的介绍与简单实现Android和H5互调的方法
![](https://img.aspxhome.com/file/2023/7/101817_0s.png)
java 线程中start方法与run方法的区别详细介绍
C#实现扫描枪扫描二维码并打印(实例代码)
SpringIOC BeanDefinition的加载流程详解
![](https://img.aspxhome.com/file/2023/8/59938_0s.png)
如何为Repository添加自定义方法
![](https://img.aspxhome.com/file/2023/4/88214_0s.png)
SpringBoot打成war包在tomcat或wildfly下运行的方法
一篇文章带你入门java变量与类型
![](https://img.aspxhome.com/file/2023/3/67643_0s.png)
C#中的Socket编程详解
![](https://img.aspxhome.com/file/2023/4/88374_0s.gif)
Java利用POI读取、写入Excel的方法指南
![](https://img.aspxhome.com/file/2023/9/59899_0s.png)
Java设计模式之原型模式详解
![](https://img.aspxhome.com/file/2023/9/88599_0s.png)
JAVA JVM面试题总结
![](https://img.aspxhome.com/file/2023/0/62390_0s.png)