How to swap two string variables in Java without u

2020-05-20 05:41发布

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 = a;
a = b;
b = temp;

But here there is a third variable. We need to eliminate the use of the third variable.

标签: java string swap
17条回答
We Are One
2楼-- · 2020-05-20 06:19

You can do in this way.

public static void main(String[] args) {
        // TODO Auto-generated method stub

        String a = "one";
        String b = "two";

        System.out.println(a);
        System.out.println(b);

        a = a+b;
        b = "";

        System.out.println("*************");
         b = a.substring(0, 3);
         a = a.substring(3, 6);

         System.out.println(a);
         System.out.println(b);

    }
查看更多
混吃等死
3楼-- · 2020-05-20 06:22

// taken from this answer: https://stackoverflow.com/a/16826296/427413

String returnFirst(String x, String y) {
    return x;
}

String a = "one"
String b = "two"
a = returnFirst(b, b = a); // If this is confusing try reading as a=b; b=a;

This works because the Java language guarantees (Java Language Specification, Java SE 7 Edition, section 15.12.4.2) that all arguments are evaluated from left to right (unlike some other languages, where the order of evaluation is undefined), so the execution order is:

  1. The original value of b is evaluated in order to be passed as the first argument to the function
  2. The expression b = a is evaluated, and the result (the new value of b) is passed as the second argument to the function
  3. The function executes, returning the original value of b and ignoring its new value
  4. You assing the result to a
  5. Now the values have been swapped and you didn't need to declare temp. The parameter x works as temp, but it looks cleaner because you define the function once and you can use it everywhere.
查看更多
虎瘦雄心在
4楼-- · 2020-05-20 06:23
public class SwapStringVariable {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a = "test";
        String b = "paper";

        a = a + b;
        b = a.substring(0, a.length()  - b.length());
        a = a.substring(b.length(), a.length());

        System.out.println(a + " " + b);


    }

}
查看更多
Summer. ? 凉城
5楼-- · 2020-05-20 06:23

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

a += b;
b = a.replaceFirst(b + "$", "");
a = a.replaceFirst("^" + b, "");
查看更多
手持菜刀,她持情操
6楼-- · 2020-05-20 06:24

The simplest way is given below:

String a = "one";
String b = "two";
System.out.println("Before swap: " a + " " + b);
int len = a.length();
a = a + b;
b = a.substring(0, len);
a = a.substring(len);
System.out.println("After swap: " a + " " + b);
查看更多
姐就是有狂的资本
7楼-- · 2020-05-20 06:27

You can also do this by using a temp variable but in a different way:

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

temp=a.concat(b);
b=temp.substring(0,a.length());
a=temp.substring(a.length(),temp.length());

System.out.println("After Swapping A:"+a+"B:"+b);
查看更多
登录 后发表回答