I want to write a method that will check two String for identity. The first String will contain the correct answer, and the second will be partially typed manually. Therefore, I need to, as a result, the text could be highlighted in red, but not all, but the part where there is an error.

private void testOne() { int len = 0, i = 0; String one, two, ds = ""; one = "hellow world"; two = "Qellow Qorld"; while(i < two.length()) { if (one.charAt(i) != two.charAt(i)) { ds += two.charAt(i); len = i; } else { ds += two.charAt(i); } i++; } Spannable ss = new SpannableString(ds); ss.setSpan(new ForegroundColorSpan(Color.RED), len, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); numberOFresponses.setText(ss); // Вывожу результат на экран. } 

Here's what I got.

enter image description here

 int len // дает знать с какого символа начинать закрашивать. 

Perhaps you need to do this all through arrays, but I do not know how to implement it.

    1 answer 1

      SpannableString ss = new SpannableString(two); while (i<two.length()) { if (one.charAt(i)!=two.charAt(i)) ss.setSpan(new ForegroundColorSpan(Color.RED), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); i++; } numberOFresponses.setText(ss); 
    • A bit not what I need, there are two errors in the hellow world, the code found only the last one, and how to paint the first and the last? - Sergiyss
    • So should the word with an error be selected or only a symbol? - Serodv
    • The symbol in the word. - Sergiyss
    • Then everything should be fine, look for the problem in yourself - Serodv
    • Your code variant will only detect the last error in the word, and highlight the symbol in red. But how can I get him to highlight other previously found characters in words. - Sergiyss