Take the 2-minute tour ×

I am trying to find a pattern of "aaa" and replace it with "b" for example using indexOf but it isn't working correctly:

private boolean matchCase;
private boolean wholeWords;

private void containsSearchValue(String currentValue, List<MnvrFindContainer> foundList, String textToFind, int i, int j)

    if .....

    else if (matchCase && wholeWords && currentValue.contains(textToFind))

        String s = currentValue;
        String pattern = textToFind;
        int index = 0;
        int lengthOfPattern = 0;
            index = s.indexOf(pattern, index + lengthOfPattern);
            lengthOfPattern = pattern.length();

            MnvrFindContainer fContainer = new MnvrFindContainer();
            fContainer.row = i;
            fContainer.column = j;

        while (index > -1);

share|improve this question
What's wrong with str.replace("aaa", "b");? –  assylias Apr 17 '13 at 15:43
There could be different patterns like aaa, aaaa, abaaba, and I want to replace all instances of "a" with "b" for example. –  Tamara Robbins Apr 17 '13 at 15:45
then str.replace("a", "b") ?? –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Apr 17 '13 at 15:46
@TamaraRobbins In that case str.replaceAll("a", "b"); will do the magic. –  Smit Apr 17 '13 at 15:47
The code you posted has lines that are not relevant to your question (the i, j, Container, etc.) and is missing an aspect that is relevant to your question (the replacement string). Create a simple class with a simple test method that attempts to solve the problem you are facing. You will probably find the solution. otherwise, post that essentialized code here. –  Darius X. Apr 17 '13 at 15:48
show 1 more comment


Your Answer



Browse other questions tagged or ask your own question.