|
Java™ by example!
|
|
|
How do I determine whether parts of two Strings are equal?
I hope this little code is what you are looking for. This is the output of it:
part of the a-String : efg String "efg" was found in "defghijk", starts at position 1 String "efg" not found in "eghijklm"
|
StringCompare.java:
 public class StringCompare { public static void compare(String tmp, String b) { // search this tmp-String in the b-String // use the methode indexOf // this methode will return -1 if the tmp-String is not // in the b-String int pos; if ((pos = b.indexOf(tmp)) != -1) { System.out.println("String \"" + tmp + "\" was found in \"" + b +"\", starts at position " + pos); } else { System.out.println("String \"" + tmp + "\" not found in \"" + b + "\""); } } public static void main(String args[]) { String a = "abcdefgh"; String b = "defghijk"; String c = "eghijklm"; // take the part of the a-String you want to find in // the b-String, here "efg" System.out.println("part of the a-String : " + a.substring(4,7)); compare(a.substring(4,7), b); compare(a.substring(4,7), c); } }
|
Further Information
Author of answer: Uwe Billen
Comments
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|