0

I have a string like

'John is a student. He is also a researcher. He is also a human.'

I have start and end indexes of John and first He. Is there a way to replace these substrings simultaneously with x. Note that I should not replace second He because I have indexes only for the first He.

We can obviously iterate over string, copy whenever the pointer is not in the window of substring and putting x instead. But, is there a better way than this?

Also, note that indexes are non-overlapping.

4
  • What do you mean "simultaneously"? Commented May 19, 2016 at 13:53
  • I mean if you replace John first. Then the indexes of He no longer represent He. So, "simultaneously". Commented May 19, 2016 at 13:54
  • Please show what you have tried so far. Your question is unclear. Commented May 19, 2016 at 15:17
  • @AndyTurner I got the answer. Please refer to accepted answer. Commented May 19, 2016 at 15:28

4 Answers 4

3

Replacing from the end is the best solution.

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);

Credits: @AndyTurner

Sign up to request clarification or add additional context in comments.

Comments

0

I do not know about a way to change both with the index. But hey, you probably first found the index by searching for the substrings John and He. So you could skip that and use the Strings static method replaceAll.

String test = "John is a student. He is also a researcher.";
System.out.println(test.replaceAll("(John)|(He)", "x"));

In fact you are checking if (John)|(He) ("John" or "He") are in the string test. If so they are replaced by "x".

replaceAll returns a reference to a new String object looking like:

x is a student. x is also a researcher.

3 Comments

I never said I searched and got those indexes. Some API gave me those indexes.
In that case I am very curious how you got those indexes. You must have given some kind of assignment. So, what did you do to get those indexes?
Assume I have a function ArrayList giveIndexes(String s) which does some black magic and gives me those indexes. :P
0

try this :

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());

Output : John is a student. x is also a researcher.

3 Comments

Please read the question carefully. If indexes I get are of second He, then the first He should not get replaced.
it won't be replaced; the only "he" that we'll be replaced is the one with beginning index is beginIndex , in my case i used beginIndex=s.indexOf(); but you can use the beginIndex you want.
I suggest you to carefully read the question. I am not searching for He. Some black box gives me start and end indexes of John and He.
0

You can use a placeholder with the same lenght of the word to replace.Then replace the placeholders with x and get the result.

 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);

output:

x is a student. x is also a researcher.

hope this can help.

13 Comments

Great!! This is what I am looking for. Thanks. :D. A small change, though. The first "xxxx" should not be hard coded. Can you change the answer such that "xxxx" is a string of x's of length of John.
It's not clear from the question that "John" should be replaced with "xxxx" - it looks like it should be replaced with "x".
@AndyTurner, Yes. I want to replace it with x. Please answer if you can achieve that. I will be glad.
@Achilles-96 no, that's not clear: "I want to replace each character of 'John' with 'x'" describes that. Simply saying "I want to replace "John" with "x"" means replacing the J and deleting the "ohn".
It's all unnecessary though, if you just the the replaces in reverse: new StringBuilder(s).replace(secondStart, secondEnd, "x").replace(firstStart, secondStart, "x").toString();.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.