对于一个字符串和字符串的某一位置,将包括i位置在内的左侧部分移动到右边,将右侧部分移动到左边
例如:输入:ABCDEFGH 8 4
输出:FGHABCDE
/*
* 对于一个字符串和字符串的某一位置,将包括i位置在内的左侧部分移动到右边,将右侧部分移动到左边
* "ABCDEFGH,8,4" 返回:FGHABCDE
* */
public class day02{
public static String string;
public static Integer len;
public static Integer loc;
public static void main(String[] args) {
System.out.println("请按如下格式输如:"+"ABCDEFG,8,4");
Scanner scanner = new Scanner(System.in);
string = scanner.next();
len = scanner.nextInt();
loc = scanner.nextInt();
System.out.println(replaceBeginAndEnd(string, loc));
}
public static String replaceBeginAndEnd(String string,int i) {
if(string == null|| i < 0||string.length() < i) {
System.out.println(string);
}
char[] ch = string.toCharArray();
reverse(ch, 0, i);
reverse(ch,i+1,ch.length-1);
reverse(ch, 0, ch.length-1);
return new String(ch);
}
public static void reverse(char[] ch,int begin,int end) {
char temp;
while(begin < end) {
temp = ch[begin];
ch[begin] = ch[end];
ch[end] = temp;
begin++;
end--;
}
}
}