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

后端 未结 18 741
梦谈多话
梦谈多话 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:02

    String str1 = "first";
    String str2 = "second";
    
    str1 = str2+str1;
    str2 = str1.substring(str2.length(),str1.length());
    str1 = str1.substring(0,(str1.length() - str2.length()));
    
    System.out.println(str1);
    System.out.println(str2);
    

提交回复
热议问题