在 Java 中不使用多余变量交换两个字符串
public class Test {
public static void main(String[] args) {
String a = "Hello";
String b = "World";
System.out.println("Strings before swap: a = " + a + " and b = " + b);
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length());
System.out.println("Strings after swap: a = " + a + " and b = " + b);
}
}
Strings before swap: a = Hello and b = World
Strings after swap: a = World and b = Hello