• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String method "replace(char, char)"

 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple question, i am a little rusty on my java, but anyways here is my simple situation.Say i have a string "a-b-c", i just want to replace the dashes with null chracters( if its possible), so that the string comes out like "abc". I know i could string tokenize it then build another string using the tokens, but i would rather try and use this replace method if possible. any help would be great. Im having a problem getting the null chracter to work.for instance
String s1="a-b-c";
String s2=s1.replace('-','\0000');---> i dont know what to use for the null chracter (im using windows)
thanks in advance
 
Hank Haroldson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might work:
String s2=s1.replace("-","");
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey hank, it actually has to be two chars not two strings, as the params.I tryed this
char nill='\000'
String s2=s1.replace('-',nill);

but i just got the exact same string again "a-b-c", intresting indeed.If no one can think of anything i'll just go with the string tokenizer solution, its not that bad.
 
Paul Clapham
Sheriff
Posts: 21155
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You actually want to end up with a String of a different length. So forget about the "null characters", they are only a bad idea that tries to make the "replace(char, char)" method do something it should not be used for. StringTokenizer or some other solution is certainly better.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
point well noted....string tokenizer it is, thanks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by luc comeau:
hey hank, it actually has to be two chars not two strings, as the params.

In the current version of Java (1.5) there is a version of replace that can take two Strings (It's actually defined as taking two CharSequences). Time to upgrade?
 
Matti Poro
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use StringBuilder.

"public StringBuilder deleteCharAt(int index)

Removes the char at the specified position in this sequence. This sequence is shortened by one char."
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matti Poro:
You could use StringBuilder.

"public StringBuilder deleteCharAt(int index)

Removes the char at the specified position in this sequence. This sequence is shortened by one char."

Is that really a handy method? You have to find the position of the offending character, and do that repeatedly. Second, the shifting of the remaining characted will have to be done repeatedly as well, which is inefficient. It it comes done to methods like this, I would say bite the bullet and just write the damn code from scratch: copy the non-dash characters one-at-a-time from the given string to a stringbuilder. done.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, thanks for all the replies.

I actually am using 1.4.2 becuase i started my software project in it long before 1.5 came out. As for doing this task...which really isnt that big of a deal i figured out that i already wrote a method to handel this function(sort of)...anyways its a long situation to explain but i also had to handel strings such as "a.b-c-d-e.f-g" where a.b and e.f are considered a single element but a "special case", and if the offer was not in alphabetical order it needed to be.So the best route for me was using string tokenizer, add elements to a vector then sort them in place.So everything is good to go now!thanks for all your time and consideration everyone.
-Luc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic