• 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

a simple question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"..
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
waiting for response from topic starter, anyway have this too



Hope it is little big....
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may use this version if you like it

[ July 13, 2006: Message edited by: karthi keyan ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bit more simpler method


 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really Simpler One.with one drawback,Suppose swap characters occurs more than once it will not work. Eg:abcdefghijkla
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I was her plaything! And so was this 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