(我来自蟒蛇世界,所以如果我将一些术语用于常规的话,我会道歉。)
我有String
个List
的起始/结束索引要替换。没有太多细节,请考虑这个基本的模型:
String text = "my email is foo@bar.com and my number is (213)-XXX-XXXX"
List<Token> findings = SomeModule.someFnc(text);
并且Token
具有
class Token {
int start, end;
String type;
}
此List
代表我尝试编辑的敏感数据的开始和结束位置。
实际上,API返回我迭代的数据以获取:
[{ "start" : 12, "end" : 22, "type" : "EMAIL_ADDRESS" }, { "start" : 41, "end" : 54, "type" : "PHONE_NUMBER" }]
使用这些数据,我的最终目标是在这些text
对象指定的Token
中编辑令牌以获取此值:
"my email is [EMAIL_ADDRESS] and my number is [PHONE_NUMBER]"
使这个问题变得非常重要的是,替换子串的长度与它们所替换的子串的长度不一样。
我目前的行动计划是从StringBuilder
构建text
,按照起始索引的相反顺序对这些ID进行排序,然后从缓冲区的右端替换。
但有些东西告诉我应该有更好的方法......有吗?
答案 0 :(得分:9)
这种方法有效:
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
String text = "my email is foo@bar.com and my number is (213)-XXX-XXXX";
List<Token> findings = new ArrayList<>();
findings.add(new Token(12, 22, "EMAIL_ADDRESS"));
findings.add(new Token(41, 54, "PHONE_NUMBER"));
System.out.println(replace(text, findings));
}
public static String replace(String text, List<Token> findings) {
int position = 0;
StringBuilder result = new StringBuilder();
for (Token finding : findings) {
result.append(text.substring(position, finding.start));
result.append('[').append(finding.type).append(']');
position = finding.end + 1;
}
return result.append(text.substring(position)).toString();
}
}
class Token {
int start, end;
String type;
Token(int start, int end, String type) {
this.start = start;
this.end = end;
this.type = type;
}
}
输出:
my email is [EMAIL_ADDRESS] and my number is [PHONE_NUMBER]
答案 1 :(得分:3)
确保所有令牌均按start
索引以升序排序:
List<Token> tokens = new ArrayList<>();
tokens.sort(Comparator.comparing(Token::getStart));
现在,您可以替换从输入文本末尾开始的所有字符串:
public String replace(String text, List<Token> tokens) {
StringBuilder sb = new StringBuilder(text);
for (int i = tokens.size() - 1; i >= 0; i--) {
Token token = tokens.get(i);
sb.replace(token.start, token.end + 1, "[" + token.type + "]");
}
return sb.toString();
}
答案 2 :(得分:1)
从开始和结束之间提取子字符串,然后按它进行拆分。然后你得到一个包含2个元素的数组,插入你想要的内容。接下来你必须移动你的下一个字符串&#39;通过(您替换长度的前一个字符串)和(放置在其位置的字符串)之间的差异来替换ID。
代码(根据情况,令牌中的&#39; end&#39;是独占的):
public class Main {
public static void main(String... args) {
String text = "I want to replace AAA and B and scary wombat";
Token[] tokens = {new Token(18, 21, "TEST"), new Token(26, 27, "TEST"), new Token(32, 44, "TEST")};
int delta = 0;
for (Token token : tokens) {
String splitter = text.substring(token.start + delta, token.end + delta);
System.out.println("Splitter: " + splitter);
delta += token.replacement.length() - splitter.length();
String[] beforeAndAfter = text.split(Pattern.quote(splitter));
text = beforeAndAfter[0] + token.replacement +
(beforeAndAfter.length == 2 ? beforeAndAfter[1] : ""); // in case where there are no more chars after splitter in text
}
System.out.println(text);
}
static class Token {
public final int start, end;
public final String replacement;
public Token(int start, int end, String replacement) {
this.start = start;
this.end = end;
this.replacement = replacement;
}
}
}