我有一个字符串
String s = "#@#:g#@# hello #@#:(#@# How are You";
#@#:g#@# 是表情符号的代码。类似下一个#@#:(#@# 是另一个代码。
现在我的字符串有几个以#@# 开头并以#@# 结尾的代码。现在我需要用另一个字符串“(情绪)”替换所有以#@# 开头并以#@# 结尾的子字符串。
Input String = "#@#:g#@# hello #@#:(#@# How are you";
Output String = "(emotions) hello (emotions) How are You".
我试过这段代码
System.out.println("Original String is = "+s);
if(s.contains("#@#"))
{
//int startIndex = s.substring(beginIndex, endIndex)
int index = s.indexOf("#@#");
while (index >= 0) {
System.out.println("index is == "+index);
arr.add(index);
index = s.indexOf("#@#", index + 1);
}
for(int i = 0 ; i<arr.size() ; i=i+2)
{
int startIndex = arr.get(i);
int secondIndex = arr.get(i+1);
System.out.println("StartIndex is == "+startIndex);
System.out.println("SecondIndex is == "+secondIndex);
String s1 = s.substring(startIndex,secondIndex+3);
System.out.println("String to be replaced is == "+s1.toString());
s.replace(s1, "(emotions)"); //\"(emotions)\"
System.out.println("String == "+s.toString());
}
}
System.out.println("Final String is == "+s.toString());
}
请帮我。