Home > Back-end >  Compare the characters of two strings and return the index of the character found
Compare the characters of two strings and return the index of the character found

Time:02-13

I'm trying to compare the characters of two Strings

  • String string1 = "ABC";
  • String string2 = "ABCCB";

so here for example if we compare the strings together , 'A' in string1 is found in index 0 in string1 'B' in index '1' and so long,so by comparing the strings I should get a number which is 01221, I know in java it has the contains() , indexOf() , but I can't think of how this can be achieved

CodePudding user response:

Iterate over your string2 char by char and use string1.indexOf(c). You'll get the sequence of numbers.

  •  Tags:  
  • java
  • Related