swap a string in java
Code snippet on how to swap a string in java
public static void swapString(String a, String b) {
String t;
t = a;
a = b;
b = t;
}
This code swaps two strings, ‘a’ and ‘b’. First, it creates a temporary, ‘t’ string which will store the value of ‘a’. Then, it sets ‘a’ to the value of ‘b’. Finally, it sets ‘b’ to the value that was in the ‘t’ temporary string (which was the value of ‘a’). Essentially, this code is exchanging the values of ‘a’ and ‘b’ by using the temporary, ‘t’, string.