字符串反转操作

/**

字符串反转操作

1.字符串变数组

2.反转数组

3.将数组转换成字符串

*/

class StringTest 

{


public static String reverseString(String s , int start , int end){

//字符串变数组

char[] chars = s.toCharArray();

//反转数组

reverse(chars,start,end);

//将数组转换成字符串

return new String(chars);


}


private static void reverse(char[] chars, int start , int end){

for(int x = start , y = end - 1 ; x < y ; x++, y--){

swap(chars , x , y);

}

}


private static void swap(char[] chars, int x , int y){

char temp = chars[x];

chars[x] = chars[y];

chars[y] = temp; 

}


public static void main(String[] args) 

{

String hello = "Hello World!";

System.out.println(hello);

System.out.println(StringTest.reverseString(hello,0,5));

}

}


 

你可能感兴趣的:(字符串)