`
wststar
  • 浏览: 17001 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

字符串反转操作

 
阅读更多

/**

字符串反转操作

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));

}

}


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics