How to swap two string variables in Java without using a third variable

后端 未结 18 764
梦谈多话
梦谈多话 2021-02-02 12:30

How do I swap two string variables in Java without using a third variable, i.e. the temp variable?

String a = \"one\"
String b = \"two\"
String temp = null;
temp         


        
18条回答
  •  鱼传尺愫
    2021-02-02 13:00

    Jj Tuibeo's solution works if you add replaceFirst() and use a regular expression:

    a += b;
    b = a.replaceFirst(b + "$", "");
    a = a.replaceFirst("^" + b, "");
    

提交回复
热议问题