To count number of 1's,0's,2's in a string in java -


I'm new to Java string operation.

A string has 0, 1, showing something like 2

  string content = "0011221100";   

You can ask me a question why you do not keep it in the whole, because in my case I should only use the string.

The output can be:

  0's: 4's: 4's of 2: 2   

How can I get it?

If you only need to search for 1, 0's, 2's < / Code> then you can do something like this:

  string content = "0011221100"; Int zeros = content.length () - content.replaceAll ("0", ""). Length (); Int ones = content.length () - content.replaceAll ("1", ""). Length (); Int twos = content.length () - content.replaceAll ("2", ""). Length (); System.out.println ("0's:" + zero); System.out.println ("1:" +); System.out.println ("3:" + Doa ");    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -