Java 替换多个子字符串(基于索引)
Java Replace multiple substrings(based on indexes)
我有一个像
这样的字符串
'John is a student. He is also a researcher. He is also a human.'
我有 John
和第一个 He
的开始和结束索引。有没有办法用 x
同时替换这些子字符串。请注意,我不应该替换第二个 He
,因为我只有第一个 He
的索引。
我们显然可以遍历字符串,只要指针不在子字符串的 window 中就进行复制,并用 x
代替。但是,还有比这更好的方法吗?
另外,请注意索引是不重叠的。
试试这个:
String s="John is a student. He is also a researcher.";
int beginIndex=s.indexOf("He");
s=s.substring(0, beginIndex)+"x"+s.substring(beginIndex+2,s.length());
输出:约翰是一名学生。 x也是研究员
我不知道用索引改变两者的方法。但是,嘿,您可能首先通过搜索子字符串 John 和 He 找到了索引。所以你可以跳过它并使用字符串静态方法 replaceAll
.
String test = "John is a student. He is also a researcher.";
System.out.println(test.replaceAll("(John)|(He)", "x"));
事实上,您正在检查 (John)|(He)
("John" 或 "He")是否在字符串测试中。如果是这样,它们将被替换为 "x"
。
replaceAll
returns 对新字符串对象的引用,如下所示:
x 是学生。 x也是研究员
您可以使用与单词相同长度的占位符来replace.Then将占位符替换为x并得到结果。
String s = "John is a student. He is also a researcher.";
int firstStart = 0,firstEnd =4,secondStart=19,secondEnd=21;
String firstPlaceholder = String.format("%"+(firstEnd - firstStart )+"s", " ").replaceAll(" ", "x");
String secondPlaceholder = String.format("%"+(secondEnd -secondStart)+"s", " ").replaceAll(" ", "x");
String result = new StringBuilder(s).replace(firstStart, firstEnd, firstPlaceholder)
.replace(secondStart, secondEnd, secondPlaceholder)
.toString().replaceAll("x[x]+", "x");
System.out.println( result);
输出:
x is a student. x is also a researcher.
希望对您有所帮助。
从末尾开始替换是最好的方案
public class NE {
public Integer startIndex;
public Integer endIndex;
}
public class CustomComparator implements Comparator<NE> {
public int compare(NE n1, NE n2) {
return n1.startIndex.compareTo(n2.startIndex);
}
}
ArrayList<NE> NEList = getIndexes();
Collections.sort(NEList, ner.new CustomComparator());
String finalString = 'John is a student. He is also a researcher. He is also a human.';
for(int i=NEList.size()-1;i>=0;i--){
NE ne = ner.new NE();
ne = NEList.get(i);
finalString = new StringBuilder(finalString).replace(ne.startIndex, ne.endIndex, 'x').toString();
}
System.out.println(finalString);
致谢:@AndyTurner
我有一个像
这样的字符串'John is a student. He is also a researcher. He is also a human.'
我有 John
和第一个 He
的开始和结束索引。有没有办法用 x
同时替换这些子字符串。请注意,我不应该替换第二个 He
,因为我只有第一个 He
的索引。
我们显然可以遍历字符串,只要指针不在子字符串的 window 中就进行复制,并用 x
代替。但是,还有比这更好的方法吗?
另外,请注意索引是不重叠的。
试试这个:
String s="John is a student. He is also a researcher.";
int beginIndex=s.indexOf("He");
s=s.substring(0, beginIndex)+"x"+s.substring(beginIndex+2,s.length());
输出:约翰是一名学生。 x也是研究员
我不知道用索引改变两者的方法。但是,嘿,您可能首先通过搜索子字符串 John 和 He 找到了索引。所以你可以跳过它并使用字符串静态方法 replaceAll
.
String test = "John is a student. He is also a researcher.";
System.out.println(test.replaceAll("(John)|(He)", "x"));
事实上,您正在检查 (John)|(He)
("John" 或 "He")是否在字符串测试中。如果是这样,它们将被替换为 "x"
。
replaceAll
returns 对新字符串对象的引用,如下所示:
x 是学生。 x也是研究员
您可以使用与单词相同长度的占位符来replace.Then将占位符替换为x并得到结果。
String s = "John is a student. He is also a researcher.";
int firstStart = 0,firstEnd =4,secondStart=19,secondEnd=21;
String firstPlaceholder = String.format("%"+(firstEnd - firstStart )+"s", " ").replaceAll(" ", "x");
String secondPlaceholder = String.format("%"+(secondEnd -secondStart)+"s", " ").replaceAll(" ", "x");
String result = new StringBuilder(s).replace(firstStart, firstEnd, firstPlaceholder)
.replace(secondStart, secondEnd, secondPlaceholder)
.toString().replaceAll("x[x]+", "x");
System.out.println( result);
输出:
x is a student. x is also a researcher.
希望对您有所帮助。
从末尾开始替换是最好的方案
public class NE {
public Integer startIndex;
public Integer endIndex;
}
public class CustomComparator implements Comparator<NE> {
public int compare(NE n1, NE n2) {
return n1.startIndex.compareTo(n2.startIndex);
}
}
ArrayList<NE> NEList = getIndexes();
Collections.sort(NEList, ner.new CustomComparator());
String finalString = 'John is a student. He is also a researcher. He is also a human.';
for(int i=NEList.size()-1;i>=0;i--){
NE ne = ner.new NE();
ne = NEList.get(i);
finalString = new StringBuilder(finalString).replace(ne.startIndex, ne.endIndex, 'x').toString();
}
System.out.println(finalString);
致谢:@AndyTurner