//对字符串指定部分进行反转;
public class ReverseDemo
{
public static void main(String[] args)
{
method1();
method2();
method3();
}
public static void method1()
{
String str = "kkdjfkdjfk";
int start = 2; //从第3个字符开始;
int end = 6; //到第7个字符结束;
String str1 = str.substring(0,start);
for(int i=end; i>=start; i--)
{
char c = str.charAt(i);
str1 += c;
}
str1 += str.substring(end+1);
System.out.println(str);
System.out.println(str1);
}
public static void method2()
{
String str = "kkdjfkdjfk";
int start = 2; //从第3个字符开始;
int end = 6; //到第7个字符结束;
char[] c = str.toCharArray();
System.out.println(str);
for(int i=start, j=end; i<j; i++,j--)
{
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}
str = new String(c);
System.out.println(str);
}
public static void method3()
{
String str = "kkdjfkdjfk";
int start = 2; //从第3个字符开始;
int end = 6; //到第7个字符结束;
StringBuffer buf = new StringBuffer();
StringBuffer buf1 = new StringBuffer();
buf.append(str);
buf1.append(buf.substring(start, end+1));
buf1.reverse();
buf.delete(start, end+1);
buf.insert(start, buf1);System.out.println(buf.charAt(0));
System.out.println(buf);
System.out.println(str);
}
}