字符串调整后比较

字符串练习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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值