Java: Issue when replacing Strings on loop -
i'm building small app auto translates boolean queries in java.
this code find if query string contains word , if so, replaces translated value.
int howmanytimes = originalvalues.size(); (int y = 0; y < howmanytimes; y++) { string originalword = originalvalues.get(y); system.out.println("original word = " + originalword); if (toreplace.contains(" " + originalword.tolowercase() + " ") || tocheck.contains('"' + originalword.tolowercase() + '"')) { toreplace = toreplace.replace(originalword, translatedvalues.get(y).tolowercase()); system.out.println("replaced " + originalword + " " + translatedvalues.get(y).tolowercase()); } system.out.println("to replace inside loop " + toreplace); }
the problem when query has, example, '(mykeyword or "blue mykeyword")' , translated values different, example, mykeyword translates to elpalavra , "blue mykeyword" translates to "elpalavra azul". happens in case result string '(elpalavra or "blue elpalavra")' when should '(elpalavra or "elpalavra azul")' . understand in first loop replaces keywords , in second no longer contains original value should translation. how can fix this?
thank you
you can sort originalvalues size desc. , after loop through them. way first replace "blue mykeyword" , after replace "mykeyword"
Comments
Post a Comment