This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes a simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a simple question" Watch "a simple question" New topic
Author

a simple question

gayathriy Nandakumar
Greenhorn

Joined: Jul 11, 2006
Posts: 15
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

Joined: Mar 02, 2005
Posts: 580
Try this

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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

Joined: Mar 02, 2005
Posts: 580
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
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
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'.


Ping & DNS - my free Android networking tools app
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
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

Joined: Mar 02, 2005
Posts: 580
waiting for response from topic starter, anyway have this too



Hope it is little big....
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
You may use this version if you like it

[ July 13, 2006: Message edited by: karthi keyan ]

http://www.skillassert.com


abhijit Ohal
Ranch Hand

Joined: Mar 10, 2006
Posts: 64
bit more simpler method


vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Really Simpler One.with one drawback,Suppose swap characters occurs more than once it will not work. Eg:abcdefghijkla
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
GeeCON Prague 2014
 
subject: a simple question