File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "strings" Watch "strings" New topic


kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
I have a set of strings. Some have a '\' in it. Some don't. How do I check a string to see if it contains a '\', cause if it doesn't, I want to leave the string as is, otherwise I want to remove the '\', create two substrings and remove the extra space. TIA.
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Use the "indexOf()" method to find the position of a character in the String.
int slashPos = s.indexOf( '\' );
If it is not found, the position will equal -1. Then use the "substring()" method to break it out.
if ( slashPos != -1 )
firstPart = s.substring( 0 , slashPos );
secondPart = s.substring( slashPos + 1 );

Wait a minute, I'm trying to think of something clever to say...<p>Joel
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Are you writing these slashes yourself?
In a string, the "\" character is used to start an escape sequence. In a string literal the slash must be followed by either b,t,n,f,r,", or ', or another \ if you want to interpret that as an actual slash character. Any other character after the \ is a compiler error.

SCJP 1.4
I agree. Here's the link:
subject: strings
It's not a secret anymore!