> I need to strip all of the pipe-characters of a string. normally I
> would use something like:
> str.replaceAll("the-char-i-want-to-replace", "");
> But from some reason: str.replaceAll("|", "") do not alter the string.
Strings are immutable so no method will alter the string.
Second, I suggest you read the docs at
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceAll(java.la
ng.String,%20java.lang.String)
The first string is a regexp and as it happens, a pipe symbol is a meta
character. So you need to escape it.
> currently I've wrote something like that:
>
[quoted text clipped - 6 lines]
> But is there any alteration I can make in order to use the much more
> readable replaceAll method ?
See above.
Kind regards
robert
Tom Gur - 14 May 2007 12:28 GMT
> > I need to strip all of the pipe-characters of a string. normally I
> > would use something like:
[quoted text clipped - 26 lines]
>
> robert
Thanks, found it.
Since Java doesn't accept "\|", you can just type "\\|" so the regular
expression will read the pipe literally.
So, str.replaceAll("\\|", "") works fine.
Piotr Kobzda - 14 May 2007 12:51 GMT
> So, str.replaceAll("\\|", "") works fine.
Just as str.replace("|", "") do.
piotr
Tom Gur - 14 May 2007 13:34 GMT
actually replace() receives 2 chars, not strings - so str.replace("|",
"") is illegal.
Piotr Kobzda - 14 May 2007 13:41 GMT
> actually replace() receives 2 chars, not strings - so str.replace("|",
> "") is illegal.
Since Java 5, it's legal as well.
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.
CharSequence,%20java.lang.CharSequence)>
piotr
Tom Gur - 14 May 2007 13:42 GMT
> > actually replace() receives 2 chars, not strings - so str.replace("|",
> > "") is illegal.
[quoted text clipped - 4 lines]
>
> piotr
ok, thanks :)
>But from some reason: str.replaceAll("|", "") do not alter the string.
>currently I've wrote something like that:
see http://mindprod.com/jgloss/string.html
The catch is, replaceAll is use regexes.
see http://mindprod.com/jgloss/regex.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com