A
A
Amigo20192020-10-25 19:09:52
Java
Amigo2019, 2020-10-25 19:09:52

How to make the number of gaps in the replacement equal to the length of the word or do a recalculation so that the replacement does not collapse?

Good afternoon!
It is necessary to replace with emptiness / remove words from the array in the string.
I find the initial indexes, and then I replace it with a void, but my replacements distort the text - how can I fix this?

ublic class Keywords {
    public static String searchAndReplaceKeywords(String afterD){
        String codeAfterSC="";
        Lexemes instanceLexemes = new Lexemes();
        StringInStringSearch instanceSearch = new StringInStringSearch();
        StringBuilder builder=new StringBuilder(afterD);
        for (int i = 0; i < instanceLexemes.stringKeywords.length; i++) {
            String word = instanceLexemes.stringKeywords[i];
            int size=word.length();
            System.out.println("Keyword :     " + word + "  finded in places: " + instanceSearch.findString(afterD, word));
            List<Integer> list=instanceSearch.findString(afterD, word);
            for(int j=0;j< list.size();j++){
                int indexDelete=list.get(j);
                int end=indexDelete+size;
                builder.replace(indexDelete,end,"    ");
            }
        }
        return codeAfterSC=builder.toString();

    }
}

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-10-25
@wataru

Expand the loop where you iterate (j) over the occurrences of the keyword. If to delete from the end indexes will not move out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question