wood burning stoves 2.0*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "strings" Watch "strings" New topic
Author

strings

kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
Hi,
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.
kevin
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 );
}
HTH


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

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.
Rob


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strings