gpt4 book ai didi

java - String replaceAll 不替换 i++;

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:38:06 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com