package str.exercise9;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="shy say123456";
// 测试substring方法效果
// System.out.println(str.substring(2));
// System.out.println(str.substring(2,5));//左闭右开
System.out.println(Test.moveToLeft(str, 2));//左移两位
System.out.println(Test.moveToRight(str, 2));//右移两位
}
//返回左移n位字符串方法
private static String moveToLeft(String str,int position) {
String str1=str.substring(position);
String str2=str.substring(0, position);
return str1+str2;
}
//返回右移n位字符串方法
private static String moveToRight(String str,int position) {
String str1=str.substring(str.length()-position);
String str2=str.substring(0, str.length()-position);
return str1+str2;
}
}
初始字符串为:str="shy say123456";
左移后为:y say123456sh
右移后为:56shy say1234
用到的方法为String类的substring方法