Blogger

Delete comment from: Javarevisited

Anonymous said...

---------------
String a;
String b;

Now do it. Fail.
---------------

String a = "abc";
String b = "def";

System.out.println("\na: " + a + "\nb: " + b);
b = a.length() + "_" + a + b;
System.out.println("\na: " + a + "\nb: " + b);
a = b.substring(Integer.parseInt(b.split("_")[0]) + b.indexOf("_") + 1);
System.out.println("\na: " + a + "\nb: " + b);
b = b.substring(b.indexOf("_") + 1, b.indexOf("_") + 1 + Integer.parseInt(b.split("_")[0]));
System.out.println("\na: " + a + "\nb: " + b);

========
Output:

a: abc
b: def

a: abc
b: 3_abcdef

a: def
b: 3_abcdef

a: def
b: abc

Win.

Feb 26, 2013, 12:26:10 PM


Posted to 3 ways to swap two Numbers without using Temp or Third Variable in Java?

Google apps
Main menu