title: Match Multiple Strings in Android
body: <p>I have three Strings</p>
  
  <p>String 1= "Hello how are you how was your day"</p>
  
  <p>String 2= "Hello how are you"</p>
  
  <p>String 3= "how was your day"</p>
  
  <p>I want to match each word of string 2 with string 1 and change colour of word accordingly. I have used below code and it's working fine </p>
  
  <pre><code>private void printDiff(final Context context, String sentence1, String sentence2) {
          String[] array1 = sentence1.split(" ");
          String[] array2 = sentence2.split(" ");
  
          SpannableStringBuilder sb = new SpannableStringBuilder(sentence1);
          for (int i = 0; i &lt; array1.length; i++) {
              int colorRes;
              if (i &lt; array2.length) {
  
                      colorRes = array1[i].equalsIgnoreCase(array2[i]) ? R.color.colorPrimary : R.color.colorAccent;
  
  
              } else {
                  colorRes = R.color.black;
              }
              int startIndex = getStartIndexOf(array1, i);
              int endIndex = startIndex + array1[i].length();
              sb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, colorRes)), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          }
  
  
      } 
  
   public static int getStartIndexOf(String[] array, int index) {
          int count = 0;
          for (int i = 0; i &lt; index; i++) {
              count += array[i].length();
              count++;
          }
          return count;
      }
  </code></pre>
  
  <p>You can see output in below image</p>
  
  <p><a href="https://i.stack.imgur.com/6XqhI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6XqhI.png" alt="enter image description here"></a></p>
  
  <p>Now i want to match string 3 with string 1 and i want output like below image because string 2 and string 1 already matched.</p>
  
  <p><a href="https://i.stack.imgur.com/JTtDf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JTtDf.png" alt="enter image description here"></a></p>
  
  <p>Can anyone please help me. I don't know how to do this.</p>