Java 替换字符串中的回车换行符的方法
时间:2022-10-05 10:55:22
使用正则表达式进行替换:
代码片段:
String documentTxt = EntityUtils.toString(entity,"gbk");//获取数据
documentTxt=documentTxt.replaceAll("[\\t\\n\\r]", "");//将内容区域的回车换行去除
说明:String类的replaceAll就有正则替换功能。 \t为制表符 \n为换行 \r为回车
java正则使用:
示例方法:
public void parseTxt(String content){
Pattern p = Pattern.compile(Config.articlePtn);
Matcher matcher = p.matcher(content);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
说明:只需记住Pattern类,它的静态方法complie解析一个正则表达式生成一个Pattern对象。
然后用模型去匹配字符串,得到一个Matcher,通过matcher的find方法遍历所有的匹配。
group为正则表达式中的组,及()表达式。group(0)为原字符串,gourp(1)为匹配到的第一个组...即匹配到的组的索引从1开始。
标签:字符串,回车换行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
eclipse springboot工程打war包方法及再Tomcat中运行的方法
2023-04-14 09:21:40
![](https://img.aspxhome.com/file/2023/9/78349_0s.png)
Java即将引入新对象类型来解决内存使用问题
2023-03-30 18:12:42
![](https://img.aspxhome.com/file/2023/5/70935_0s.png)
详解Spring Boot Profiles 配置和使用
2021-10-05 22:54:57
SpringBoot使用自动配置xxxAutoConfiguration
2022-11-20 09:05:56
Android利用ViewPager实现滑动广告板实例源码
2021-12-17 18:48:15
![](https://img.aspxhome.com/file/2023/8/116158_0s.png)
Android简单实现菜单拖拽排序的功能
2023-03-10 22:32:31
Volatile关键字的使用案例
2022-01-27 16:37:05
![](https://img.aspxhome.com/file/2023/4/86054_0s.png)
Android中检查、设置默认程序详解
2021-08-25 14:14:21
![](https://img.aspxhome.com/file/2023/5/120075_0s.png)
Android SQLite数据库增删改查操作的案例分析
2022-06-06 02:55:03
![](https://img.aspxhome.com/file/2023/3/138383_0s.png)
一文搞懂Android RecyclerView点击展开、折叠效果的实现代码
2023-02-17 14:52:24
![](https://img.aspxhome.com/file/2023/1/138991_0s.gif)
Spring Cloud 配置中心内容加密的配置方法
2023-02-11 09:05:15
Android侧滑效果简单实现代码
2023-02-25 09:50:00
![](https://img.aspxhome.com/file/2023/8/138448_0s.gif)
Java中ArrayList集合的常用方法大全
2023-09-01 15:23:30
java文件操作之Path,Paths,Files
2023-11-19 15:34:34
浅谈在Spring中如何使用数据源(DBCP、C3P0、JNDI)
2022-12-14 18:11:58
Java使用OCR技术识别验证码实现自动化登陆方法
2022-03-10 04:34:52
![](https://img.aspxhome.com/file/2023/9/105959_0s.png)
Java Maven高级之插件开发详解
2023-05-11 19:10:02
![](https://img.aspxhome.com/file/2023/6/79306_0s.png)
java中TreeMap排序的示例代码
2023-02-17 15:39:30
SpringBoot使用jsr303校验的实现
2022-04-21 04:02:39
Maven 配置文件 生命周期 常用命令详解
2022-07-05 19:59:39