package itheima;
public class Demo2 {
public static void main(String[] args) {
// 创建两字符串对象
String s1 = "abc";
String s2 = "abc";
// 调用方法
boolean b = equals(s1, s2);
System.out.println("是否相同:"+b);
}
private static boolean equals(String s1, String s2) {
// 先判断长度是否相等
if (!(s1.length() == s2.length())) {
return false;
}
//字符串转成字符数组
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
//遍历比较单个是否一样
for (int i = 0; i < ch1.length; i++) {
if (!(ch1[i] == ch2[i])) {
return false;
}
}
return true;
}
}
|