字符串练习12-字符串调整后比较
//调整字符串
/*
* 给定两个字符串,A和B
* A的旋转操作就是将A最左边的字符移动到最右边
* 例如,若A=‘abcde’,在移动依次之后结果就是‘bcdea’
* 如果在若干次调整操作之后,A能变成B,那么返回True
* 如果不能匹配成功,则返回false
* */
public class Test2 {
public static void main(String[] args) {
//1.定义两个字符串
String strA="abcde";
String strB="cdeab";
//2.旋转字符串
boolean result=check(strA,strB);
//3.输出
System.out.println(result);
// for (int i = 0; i < strA.length(); i++) {
strA = rotate(strA);
if (strA.equals(strB)==true){
System.out.println(true);
break;
}if (i==strA.length()-1){
System.out.println(false);
}
}
}
public static boolean check(String strA,String strB){
for (int i = 0; i < strA.length(); i++) {
strA = rotate(strA);
if (strA.equals(strB)==true){
return true;
}
}
return false;
}
//作用:旋转字符串,把左侧的字符移动到右侧去
//形参:旋转前的字符串
//返回值:旋转后的字符串
public static String rotate(String str){
//截取
char first = str.charAt(0);
//拼接
String end = str.substring(1);
return end+first;
}
}