串的比较
比较两个串的内容是否相同的规则是:
比较对应位置的每个字母,出现一处不同,则不相同。 一个串为空时,另一个不空,则不同。
请用递归的方法,重现实现比较两个串的内容是否相同。
public class MyA
{
static boolean compare(String a, String b)
{
if(a.length() != b.length()) return false;
if(a.length()==0) return true;
if(a.charAt(0) != b.charAt(0)) return false;
return compare(a.substring(1), b.substring(1));
}
public static void main(String[] args)
{
String s1 = "abcde";
String s2 = "abcded";
System.out.println(compare(s1,s2));
}
}