Java - Counting how many characters show up in another string
<p>I am comparing two strings, in Java, to see how many characters from the first string show up in the second string.  The following is some expectations:  </p>

<pre><code>matchingChars("AC", "BA") → 1  
matchingChars("ABBA", "B") → 2  
matchingChars("B", "ABBA") → 1  
</code></pre>

<p>My approach is as follows:  </p>

<pre><code> public int matchingChars(String str1, String str2) {

    int count = 0;

     for (int a = 0; a &lt; str1.length(); a++)
    {    
        for (int b = 0; b &lt; str2.length(); b++)

          {  char str1Char = str1.charAt(a);
             char str2Char = str2.charAt(b);

                if (str1Char == str2Char)
                   {   count++;
                       str1 =  str1.replace(str1Char, '0');
                   }
          }
    }
     return count; 
    }  
</code></pre>

<p>I know my approach is not the best, but I think it should do it.  However, for</p>

<pre><code>   matchingChars("ABBA", "B") → 2  
</code></pre>

<p>My code yields "1" instead of "2".  Does anyone have any suggestion or advice? Thank you very much.</p>
