java - String replaceAll 不替换 i++;

标签 java string str-replace replaceall

String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i++;", "");

//期望的输出::newCode = "helloworld";

但这并不是用空白替换 i++。

最佳答案

只需使用 replace() 而不是 replaceAll()

String preCode = "helloi++;world";
String newCode = preCode.replace("i++;", "");

或者如果你想要replaceAll(),应用下面的正则表达式

String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i\\+\\+;", "");

注意:在 replace() 的情况下,第一个参数是字符序列,但在 replaceAll 的情况下,第一个参数是正则表达式

关于java - String replaceAll 不替换 i++;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51703641/

相关文章:

java - 在 JLabel 中绘图

java相同的ArrayList用于多表模型

Java 包含不能按预期工作,因为 "someString"!= "someString"

PHP 在插入数据库之前删除空行

php - 如何使用 PHP 从 HTML 文件中获取元数据?

regex - 使用 toOrdinal 将数字替换为序数

java - 重做 foreach 到 .stream().map

java - 使用 spring mongoTemplate 进行条件/最大 mongo 查询

javascript - 如何将javascript中的数字数组转换为字符串?

android - 蓝牙聊天 - 将完整的传入消息作为字符串传递