• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query on String class ?  RSS feed

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have Two String variables
String str ="Hello";
String str1 = "Jhon";
Now I want to swap it inbetween "str" and "str1" without using third variable i.e. when I print "str" then it should print Jhon and when I print str1 then it should print Hello.Can any one plz guide me how I can do that.
Thanks & Regards
Bikash
[ November 24, 2003: Message edited by: Bikash Paul ]
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the problem with using a temporary variable?
If you are convinced that you don't want to use one, the only way that I can think of is to define a separator character e.g. '#', "append" it to the end of one of the Strings followed by the other String and then parse the required bit of the compound String into each variable.
e.g.
String one = "one" ;
String two = "two" ;
one = one + "#" + two ;
int separatorIndex = one.indexof( '#' );
two = one.substring( 0, separatorIndex );
one = one.substring( separatorIndex );
Or something like that.
This will of course end up creating a number of new objects while during the manipulation as Strings are immutable thereby negating any performance gain that you would have achieved by not specifying an extra variable.
Jeremy.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Lot of thanks.
Thanks
Bikash
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!