将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反转为”abfedcg”
package com.nuo.testString;
public class testString_10_2 {
public static void main(String[] args) {
System.out.println(Method_String_10_2.Method_String_10_2_1(1, 3, "xiao"));
System.out.println(Method_String_10_2.Method_String_10_2_1(2, 6, "abcdefg"));
System.out.println(Method_String_10_2.Method_String_10_2_2(1, 3, "xiao"));
System.out.println(Method_String_10_2.Method_String_10_2_2(2, 6, "abcdefg"));
}
}
class Method_String_10_2{
public static String Method_String_10_2_1(int start, int end,String str){
if (str != null && str.length() != 0){
int j = start;
int k = end;
char[] ch = str.toCharArray();
for (int i = start; i < ((j + k) / 2 ); i++) {
char ch_ = ch[start];
ch[start] = ch[end - 1];
ch[end - 1] = ch_;
start ++;
end --;
}
return new String(ch);
}else {
return null;
}
}
public static String Method_String_10_2_2(int start, int end,String str){
if (str != null && str.length() != 0){
return str.substring(0,start) + new StringBuilder(str.substring(start,end)).reverse() + str.substring(end,str.length());
}else {
return null;
}
}
}