中易网

用java怎么做这道题字符串右移的问题? 字符串右移n位,例如 "hello world" 右移两位 后ldhello wor

答案:1  悬赏:60  
解决时间 2021-01-13 16:51
  • 提问者网友:你给我的爱
  • 2021-01-13 03:28
用java怎么做这道题字符串右移的问题? 字符串右移n位,例如 "hello world" 右移两位 后ldhello wor
最佳答案
  • 二级知识专家网友:思契十里
  • 2021-01-13 04:02
public class MoveString { public static void main(String[] args) { String str = "hello world"; int step = 2; System.out.println(moveString(str, step)); } private static String moveString(String str, final int step) { if (str == null || step < 0) { return null; } int len = str.length(); return new StringBuilder(str.substring(len - step % len)).append(str.substring(0, len - step % len)).toString(); } } 追问: 我怎么看不懂是什么意思呢? 回答: 你这个向右移几位其实就相当于将字符串最后的几位补到前面去...比如说你移动两位,其实就是将"hello world"最后的两位"ld"放到最前面 String类中的substring方法 就是用来进行字符串截取的

希望采纳
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息