How to swap String characters in Java?

后端 未结 14 2264
佛祖请我去吃肉
佛祖请我去吃肉 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:53

    The following line of code will swap the first two characters in str:

    return str.charAt(1) + str.charAt(0) + str.substring(2);
    

提交回复
热议问题