package com.someexercise;
import java.util.Arrays;
public class restring {
/*
字符反转
将字符串中指定部分进行反转。比如将"abcdef"反转为"aedcbf"
编写方法 public static String reverse(String str, int start , int end) 搞定
再做一些异常处理
*/
public static void main(String[] args) {
String aa = "asdqwdffasf";
System.out.println("===交换前===");
System.out.println(aa);
try {
aa = reverse(aa, 1, 0);
} catch (Exception e) {
System.out.println(e.getMessage());
return;
} finally {
}
System.out.println("===交换后===");
System.out.println(aa);
}
public static String reverse(String str, int start , int end){
if (!(str!=null && start >= 0 && start <= end && end < str.length())){
throw new RuntimeException("参数异常");
}
System.out.println("===参数正确===");
char[] aa = str.toCharArray(); //将字符串存入数组
char temp = '0';
for (int i = start,j = end;i < j ;i++,j--){
temp = aa[i];
aa[i] = aa[j];
aa[j] = temp;
}
return new String(aa);
}
}