In this program we we are using substring method to swap the String without third parameter. from substring we break string and compare from length.
public class SwapString {
public static void main(String[] args) {
//declare two string
String s1 = "hello";
String s2 = "well";
//string before swap
System.out.println("before swap: "+s1+" - "+s2);
//add second string from first
s1=s1+s2; //hellowell
s2=s1.substring(0, s1.length()-s2.length());
s1=s1.substring(s2.length());
System.out.println("after swap: "+s1+" - "+s2);
}
}
Output :-
before swap: hello – well
after swap: well – hello