chaostring
发贴: 6
积分: 0
|
于 2007-11-15 19:57
/* * This class is to compare two strings. */
package comparestring;
/** * This class contains a method to compare two strings * @author Chaotring */ public class CompareString {
/** * @param args the command line arguments */ public static void main(String[] args) { String s1 = "wel"; String s2 = "wel"; System.out.println(compStrings(s1,s2)); } public static boolean compStrings(String str1, String str2){ /* use equals() */ //return str1.equals(str2)? true: false; /* use compareTo() */ //return str1.compareTo(str2)==0? true: false; /* use getChars() */ int l1 = str1.length(); int l2 = str2.length(); char[] c1 = new char[l1]; char[] c2 = new char[l2]; boolean big = false; if(l1==l2){ str1.getChars(0, l1, c1, 0); str2.getChars(0, l2, c2, 0); for(int i = 0; i < l1; i++ ){ if(c1[i] != c2[i]){ break; }else{ big = true; } } } return big; }
}
chaostring edited on 2007-11-16 12:04
|