例如我有字符串
Cash$$$$$$Ca$$$$$$sh
左侧:Cash$$$$$$
,右侧:Ca$$$$$$sh
我想实现一种方法,如果左侧包含相等字符的序列,则返回 true,该方法等于右侧的序列。它们的值必须与它们的长度相同。 此示例返回 true。
最好写一个方法。
System.out.println(checkString("Cash$$$$$$Ca$$$$$$sh")); //false
System.out.println(checkString("Cash$$$$$$Cash$$$$$$")); //true
System.out.println(checkString("abcdabc")); // false
System.out.println(checkString("abcabc")); // true;
public static boolean checkString(String str) {
// odd length of strings can't have equals halves.
if (str.length() % 2 == 1) {
return false;
}
int mid = str.length()/2;
for (char c : str.substring(0,mid).toCharArray()) {
if (c != str.charAt(mid++)) {
return false;
}
}
return true;
}