gpt4 book ai didi

java - String.replace 后斜杠引号消失

转载 作者:行者123 更新时间:2023-12-01 12:24:34 24 4
gpt4 key购买 nike

这只是字符串替换谜语,但它已经超出了我的控制范围。

String dataToBeReplaced = "#putReplacement|Expression#";
String toBeReplacedWith = "<a href=\"google.com\" link=\"The link\"></a>";
String toBeReplacedIn = "{Content:"#putReplacement|Expression#"}";

// What I am doing is
toBeReplacedIn.replaceAll(dataToBeReplaced.replace('|','.'),toBeReplacedWith);
//What I am getting in 'toBeReplacedIn'
//{Content:"<a href="google.com" link="The link"></a>"}
**//In above output, see slashes are removed, this removal actually resulting in a string which is wrong Jason.**

请建议我如何替换我的“toBeReplacedWith”而不丢失目标字符串中的任何斜线“toBeReplacedIn”。

最佳答案

String.replaceAll方法的行为与 javadoc 所说的完全一致。

"Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired."

引用的文字(上面)甚至解释了如何解决您的问题。

关于java - String.replace 后斜杠引号消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26463381/

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