如何在不使用第三个变量的情况下在 Java 中交换两个字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18528697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:47:30  来源:igfitidea点击:

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

javastringswap

提问by user1281029

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

如何在 Java 中交换两个字符串变量而不使用第三个变量,即临时变量?

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.

但这里还有第三个变量。我们需要消除第三个变量的使用。

回答by commit

Here you go. Try this:

干得好。尝试这个:

String a = "one";
String b = "two";
//String temp=null;
int i = a.length();
a += b;
b = a.substring(0, i);
a = a.substring(i, a.length());
System.out.println(a + " " + b);

Take any value in as string in a variable. It will be swap.

将任何值作为变量中的字符串。这将是交换。

回答by Jj Tuibeo

For strings:

对于字符串:

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

a = a + b;
b = a.replace(b, "");
a = a.replace(b, "");

回答by Husman

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

a = a.concat("#" + b);
b = a.split("#")[0];
a = a.split("#")[1];

This will work as long as your string doesn't contain the # character in them. Feel free to use any other character instead.

只要您的字符串中不包含 # 字符,这就会起作用。随意使用任何其他字符。

You could use a possible Unicode character, like "\u001E" instead of the #.

您可以使用可能的 Unicode 字符,例如“\u001E”而不是 #。

回答by Ankur Lathi

Do it like this without using a third variable:

在不使用第三个变量的情况下这样做:

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

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

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

回答by Praveen P Moolekandathil

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);

回答by marcus

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

// 取自这个答案: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:

这是有效的,因为 Java 语言保证(Java 语言规范,Java SE 7 版,第 15.12.4.2 节)从左到右评估所有参数(与某些其他语言不同,评估顺序未定义),因此执行顺序是:

  1. The original value of bis evaluated in order to be passed as the first argument to the function
  2. The expression b = ais 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 band 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 xworks as temp, but it looks cleaner because you define the function once and you can use it everywhere.
  1. 的原始值b被评估以便作为第一个参数传递给函数
  2. 计算表达式b = a,并将结果( 的新值b)作为第二个参数传递给函数
  3. 函数执行,返回原始值b并忽略其新值
  4. 你将结果评估为 a
  5. 现在值已交换,您无需声明temp. 该参数的x作用是temp,但它看起来更简洁,因为您定义了一次函数,并且可以在任何地方使用它。

回答by Couchcamote

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);


    }

}

回答by armysheng

回答by troy

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

如果您添加 replaceFirst() 并使用正则表达式,则Jj Tuibeo 的解决方案有效:

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

回答by Nirav Dhinoja

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);