aspose file tools*
The moose likes Beginning Java and the fly likes String method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String method "replace(char, char)"" Watch "String method "replace(char, char)"" New topic
Author

String method "replace(char, char)"

luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
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


National Research Council<br />Internet Logic Department
Hank Haroldson
Greenhorn

Joined: Jan 04, 2006
Posts: 9
This might work:
String s2=s1.replace("-","");
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
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
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

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

Joined: Jan 20, 2005
Posts: 97
point well noted....string tokenizer it is, thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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?


There is no emoticon for what I am feeling!
Matti Poro
Ranch Hand

Joined: Dec 03, 2005
Posts: 41
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 20, 2005
Posts: 97
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String method "replace(char, char)"