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。

方法四:递归法(分治思想)

public static String reverse1(String s) {
  int length = s.length();
  if (length <= 1){
     return s;
    }
  String left = s.substring(0, length / 2);
  String right = s.substring(length / 2, length);
  return reverse1(right) + reverse1(left);
 }

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值