gpt4 book ai didi

java - 将字符串中的字符替换为其他字符会产生异常

转载 作者:行者123 更新时间:2023-11-30 05:38:49 24 4
gpt4 key购买 nike

我正在使用正则表达式提取一些值,比如 "Ehj113GPTAw9RMbTgxce2jqh+2rn7tLWFz5JHyhxa+K68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH"

我想将“+”替换为“%2B”并将其发送到第二个请求:"Ehj113GPTAw9RMbTgxce2jqh%2B2rn7tLWFz5JHyhxa%2BK68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH"

我怎样才能实现这个目标?

我已经尝试过replaceAllreplace但我在某个地方出错了。

var Connect = vars.get("92_ConnectionToken").toString();
log.info(Connect);
String ConnectToken = Connect.replaceAll("+", "%2B");
String ConnectFinal = ConnectToken.replaceAll("/", "%2F");
vars.put("92_ConnectionToken", ConnectFinal);
log.info(ConnectFinal);

预期结果:

Ehj113GPTAw9RMbTgxce2jqh%2B2rn7tLWFz5JHyhxa%2BK68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH.

该字符串应作为第二个请求的输入发送。

实际结果:

2019-05-13 10:37:22,343 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: `` var Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); Str . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll
2019-05-13 10:37:22,343 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: `` var Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); Str . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll.

最佳答案

如果您想使用 String 特定方法作为 replaceAll,请将变量定义为 String:

String Connect = vars.get("92_ConnectionToken");

您需要将正则表达式特殊字符转义为 +

String ConnectToken = Connect.replaceAll("\\+", "%2B");

正如@kumesana评论的那样,你最好使用replace方法:

String ConnectToken2 = Connect.replace("+", "%2B");

关于java - 将字符串中的字符替换为其他字符会产生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56106227/

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