How to swap String characters in Java?

后端 未结 14 2208
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 14:22

How can I swap two characters in a String? For example, \"abcde\" will become \"bacde\".

14条回答
  •  醉梦人生
    2020-12-08 14:52

    StringBuilder sb = new StringBuilder("abcde");
        sb.setCharAt(0, 'b');
        sb.setCharAt(1, 'a');
        String newString = sb.toString();
    

提交回复
热议问题