• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Query on String class ?

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Lot of thanks.
Thanks
Bikash
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic