天天看点

Java:字符串反转的三种方式Java:字符串反转的三种方式

Java:字符串反转的三种方式

方式一:转换为char[]数组

//方式一:转换为char[]数组
public class reverseString{
    public String reverse(String str,int startIndex,int endIndex){
        if(str!=null){
            //调用String类的toCharArray方法将字符串转换为char[]数组
            char[] arr = str.toCharArray();
            //反转指定的字符串
            for(int x=startIndex,y=endIndex;x<y;x++,y--){
                char temp = arr[x];
                arr[x]=arr[y];
                arr[y]=temp;
            }
            //调用String构造器,将char[]数组转换为String类型返回
            return new String(arr);
        }else
            throw new RuntimeException("输入字符串为空!");
    }
    @Test
    public void test(){
        String str = "abc123";
        String reStr = reverse(str,0,4);
        System.out.println(reStr);
    }
}
           

方式二:使用拼接操作

//方式二:使用拼接操作
public class reverseString1 {
    public String reverse(String str,int startIndex,int endIndex){
        if(str!=null){
            //String subString(int beginIndex,int endIndex):
            // 返回一个新的字符串,begin包含,end不包含
            String reverseStr = str.substring(0,startIndex);
            for(int i = endIndex;i>=startIndex;i--){
                //char charAt(int index):返回某索引处的字符
                reverseStr+=str.charAt(i);
            }
            //String subString(int beginIndex):
            // 返回一个新的字符串,它是此字符串从begin位置开始的字符串
            reverseStr+=str.substring(endIndex+1);
            return reverseStr;
        }else throw new RuntimeException("字符串为空!");
    }
    @Test
    public void test(){
            String Str = "abc123";
            String reverseStr = reverse(Str,0,3);
            System.out.println(reverseStr);
    }
}

           

使用方法:

  1. subString(int startIndex,int endIndex):返回一个从startIndex到endIndex的字符串,需要注意的是startIndex包含,endIndex不包含。
  2. subString(int startIndex):返回一个从startIndex开始到字符串结束位置的字符串。
  3. chaAt(int Index):返回指定Index位置的字符。

方式三:使用StringBuffer/StringBuilder替换String

//方式三:使用StringBuilder/StringBuffer替换String
public class reverseString2 {
    public String reverse(String Str,int startIndex,int endIndex){
        if (Str!=null){
            //创建指定长度的StringBuilder对象
            StringBuilder reverseStr = new StringBuilder(Str.length());
            //使用StringBuilder类的append方法:相当于"+"
            reverseStr.append(Str.substring(0,startIndex));
            for (int i = endIndex; i >=startIndex ; i--) {
                reverseStr.append(Str.charAt(i));
            }
            reverseStr.append(Str.substring(endIndex+1));
            //StringBuilder类中重写了toString:返回String类型的数据
            return reverseStr.toString();

        }else throw new RuntimeException("输入字符串为空!");
    }
    @Test
    public void test(){
        String str = "abc123";
        String reverseStr = reverse(str,0,3);
        System.out.println(reverseStr);
    }
}
           

StringBuilder、StringBuffer的选择需要注意的问题:

  1. StringBuilder、StringBuffer中的方法和功能是完全等价的。
  2. StringBuffer中的方法基本采用synchronized关键字修饰,因此是线程安全的;而StringBuilder没有这个修饰,因此是线程不安全的。
  3. StringBuilder的执行效率高于StringBuffer。

以上便是个人总结的反转指定字符串的方法,单线程情况下推荐使用第三种使用StringBuilder类的方法,如果各位大牛有更好的方法,欢迎分享。