concat方法和indexOf方法(1206回环变位)

concat方法是一个拼接方法。
看API中写的是这样的:




这个例子就是非常形象。

关于concat(String str) 与 +号的不同的参考博客: http://www.iteye.com/problems/41712


CODE.indexOf(String s)方法是检索s是否在CODE中出现,出现的位置是第几个。或者是s字符串出现的首地址位置是多少,返回的是一个整型。


这里我们使用将目标字符串s再次拼接一次,然后用indexOf()检测就可以了。实现起来很简单,而且效果非常好。

代码如下:
public static void Loopback (){
String s = StdIn. readLine () ;
String t = StdIn. readLine () ;
if (s.length() == t.length())
if (s.concat(s).indexOf(t) >= 0 ){ //indexof()是一个查找是否存在有字符串的方法
StdOut. println (s.concat(s)) ;
StdOut. println (s.concat(s).indexOf(t)) ;
StdOut. println ( s + " is the circulor rotation of " + t) ;
} else {
StdOut. println ( s + " is not the circulor rotation of " + t) ;
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值