(I come from the python world, so I apologise if some of the terminology I use jars with the norm.)
I have a String
with a List
of start/end indices to replace. Without getting too much into detail, consider this basic mockup:
String text = "my email is foo@bar.com and my number is (213)-XXX-XXXX"
List<Token> findings = SomeModule.someFnc(text);
And Token
has the definition of
class Token {
int start, end;
String type;
}
This List
represents start and end positions of sensitive data that I'm trying to redact.
Effectively, the API returns data that I iterate over to get:
[{ "start" : 12, "end" : 22, "type" : "EMAIL_ADDRESS" }, { "start" : 41, "end" : 54, "type" : "PHONE_NUMBER" }]
Using this data, my end goal is to redact the tokens in text
specified by these Token
objects to get this:
"my email is [EMAIL_ADDRESS] and my number is [PHONE_NUMBER]"
The thing that makes this question non-trivial is that the replacement substrings aren't always the same length as the substrings they're replacing.
My current plan of action is to build a StringBuilder
from text
, sort these IDs in reverse order of start indices, and then replace from the right end of the buffer.
But something tells me there should be a better way... is there?