Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Removing pipes from a string

Thread view: 
Tom Gur - 14 May 2007 11:20 GMT
Hi,

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.
currently I've wrote something like that:

 private static String strip_for_validation(String str) {
   return str.lastIndexOf('|') == -1 ? str.replaceAll(" ", "") :
strip_for_validation(str.substring(0,str.lastIndexOf('|')) +
str.substring(str.lastIndexOf('|')+1));
 }

But is there any alteration I can make in order to use the much more
readable replaceAll method ?
Robert Klemme - 14 May 2007 11:36 GMT
> 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 :)
Roedy Green - 14 May 2007 14:17 GMT
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.