可以用自定义方法啊:
- public static String reverseString(String s,int start,int end)
- {
- char[] chs = s.toCharArray();
- reverse(chs,start,end);
- return new String(chs);
- }
- public static String reverseString(String s)
- {
- return reverseString(s,0,s.length());
-
- }
- private static void reverse(char[] arr,int x,int y)
- {
- for(int start=x,end=y-1; start<end ; start++,end--)
- {
- swap(arr,start,end);
- }
- }
- private static void swap(char[] arr,int x,int y)
- {
- char temp = arr[x];
- arr[x] = arr[y];
- arr[y] = temp;
- }
- public class StringTest
- {
- public static void main(String[] args)
- {
-
- System.out.println("输出:"+reverseString("我是一个男孩"));
-
- }
- }
复制代码 |