esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

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!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.