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
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.
Joined: Jan 20, 2005
point well noted....string tokenizer it is, thanks
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.
Joined: Jan 20, 2005
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