gpt4 book ai didi

java - 多个字符串替换而不影响后续迭代中的替换文本

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

我之前发布过关于信件的帖子,但这是另一个主题,我有一个包含 2 个对象的 json 响应,fromtofrom 是要更改的内容,to 是将更改为的内容。

我的代码是:

// for example, the EnteredText is "ab b test a b" .
EnteredString = EnteredText.getText().toString();
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);

String Original = jo_inside.getString("from");
String To = jo_inside.getString("to");

if(isMethodConvertingIn){
EnteredString = EnteredString.replace(" ","_");
EnteredString = EnteredString.replace(Original,To + " ");
} else {
EnteredString = EnteredString.replace("_"," ");
EnteredString = EnteredString.replace(To + " ", Original);
}
}

LoadingProgress.setVisibility(View.GONE);
SetResultText(EnteredString);
ShowResultCardView();

例如json响应为:

{
"Response":[
{"from":"a","to":"bhduh"},{"from":"b","to":"eieja"},{"from":"tes","to":"neesj"}
]
}

String.replace() 方法在这里不起作用,因为首先它会将 a 替换为 bhduh,然后是 b eieja,但是问题来了,它将 bhduh 中的 b 转换为 eieja,这我不想。

我想根据 Json 完美地转换字符串中的字母和“单词”,但这是我失败的地方。

新代码:

if(m_jArry.length() > 0){
HashMap<String, String> m_li;

EnteredString = EnteredText.getText().toString();

Log.i("TestAf_","Before Converting: " + EnteredString);

HashMap<String,String> replacements = new HashMap<String,String>();
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);

String Original = jo_inside.getString("from");
String To = jo_inside.getString("to");

if(isMethodConvertingIn){

//EnteredString = EnteredString.replace(" ","_");

replacements.put(Original,To);
Log.i("TestAf_","From: " + Original + " - To: " + To + " - Loop: " + i);
//EnteredString = EnteredString.replace(" ","_");
//EnteredString = EnteredString.replace(Original,To + " ");

} else {

EnteredString = EnteredString.replace("_"," ");
EnteredString = EnteredString.replace("'" + To + "'", Original);
}

}
Log.i("TestAf_","After Converting: " + replaceTokens(EnteredString,replacements));

// Replace Logic Here
// When Finish, Do :
LoadingProgress.setVisibility(View.GONE);
SetResultText(replaceTokens(EnteredString,replacements));
ShowResultCardView();

输出:

10-10 19:51:19.757 12113-12113/? I/TestAf_: Before Converting: ab a ba
10-10 19:51:19.757 12113-12113/? I/TestAf_: From: a - To: bhduh - Loop: 0
10-10 19:51:19.757 12113-12113/? I/TestAf_: From: b - To: eieja - Loop: 1
10-10 19:51:19.757 12113-12113/? I/TestAf_: From: o - To: neesj - Loop: 2
10-10 19:51:19.758 12113-12113/? I/TestAf_: After Converting: ab a ba

最佳答案

如果您给出函数的预期输出,您的问题会更清楚。

假设是:ab b test a b >>>> bhduheieja eieja neesjt bhduh eieja

然后看下面,Javadoc中的重点是“This will not repeat”

http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#replaceEach(java.lang.String,%20java.lang.String[],%20java.lang.String[])

Replaces all occurrences of Strings within another String. A null reference passed to this method is a no-op, or if any "search string" or "string to replace" is null, that replace will be ignored. This will not repeat. For repeating replaces, call the overloaded method.

示例 1

import org.apache.commons.lang3.StringUtils;

public class StringReplacer {

public static void main(String[] args) {

String input = "ab b test a b";
String output = StringUtils.replaceEach(input, new String[] { "a", "b", "tes" },
new String[] { "bhduh", "eieja", "neesj" });

System.out.println(input + " >>>> " + output);
}
}

示例 2

import org.apache.commons.lang3.StringUtils;

public class StringReplacer {

public static void main(String[] args) {

String input = "this is a test string with foo";
String output = StringUtils.replaceEach(input, new String[] { "a", "foo" },
new String[] { "foo", "bar"});

System.out.println(input + " >>>> " + output);
}
}

关于java - 多个字符串替换而不影响后续迭代中的替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39963316/

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