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

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

    String name = "george";
    String name2 = "mark";
    System.out.println(name+" "+name2);
    System.out.println(name.substring(name.length()) + name2 +  " "+ name );
    

    Here substring(); method returns empty string. hence , names can be appended.

提交回复
热议问题