Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a simple question

 
gayathriy Nandakumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.. i have a question regarding Swapping of characters in a string that uses stringbuffer..
like
StringBuffer codestring="abcdefghijkl"..how do i swap the char 'a' with 'd' in the string so that final string is like..
codestring="dbcaefghijkl"..
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vidya sagar:
Try this



Dear Vidya,

This will only replace 'a' to 'd' not the 'd' to 'a'. He might want to replace these 2 chars simultaneously.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will only replace 'a' to 'd' not the 'd' to 'a'. He might want to replace these 2 chars simultaneously.


Hi Ankur

i just seen how do i swap the char 'a' with 'd' in the string this sentence.Poster doesnot say about vice versa.If he/she wants wants then we will come with some other solution.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i swap the char 'a' with 'd' in the string this sentence.Poster does not say about vice versa

I'm not a native English-speaker, but I think swap is not the same as replace, which is what you are referring to. Swap indeed means that the two items in questions switch their places, so there really is no "vice versa" - both happen at the same time.

One possibility would be to replace 'a' by a completey different character -which you can be sure does not otherwise occur in the string-, then replace 'd' by 'a', and then the other character by 'd'.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,You are correct.But Anyway i am looking for the real poster to reply on this regard.BY the way i am also not native Speaker of English.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waiting for response from topic starter, anyway have this too



Hope it is little big....
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may use this version if you like it

[ July 13, 2006: Message edited by: karthi keyan ]
 
abhijit Ohal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bit more simpler method


 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really Simpler One.with one drawback,Suppose swap characters occurs more than once it will not work. Eg:abcdefghijkla
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you will also note that the original poster said

how do i make this:
abcdefghijkl

look like this:
dbcaefghijkl

note that here it's quite clear they mean "make all 'a's in the original into 'd's, and all 'd's into 'a's...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a replace based on REXX syntax. Note how it can be used to rearrange a string. You could use that to swap characters based on position. I bet it would not give correct results on translate AD to DA.

This is very old code with a semi-hungarian prefix naming thing that I outgrew ...

The left() method pads or truncates as necessary to left justify.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic