Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with indexOf()

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
I'm using the following code to detect the presence of a backslash:
String slash = path.substring(0, path.indexOf("/"));
int x = slash.length();

And then I want to use the result in an If statement...If(x>0)...but if 'path' doesn't contain a backslash, it errors saying the string is out of range, which I believe is because if the character doesn't exist it returns -1. Is there a way I can use this method when characters don't exist?
thanks!
 
a) backslash or slash?
b)

int pos = path.indexOf("/"));
if (pos == -1)
/* I don't know what you like to do, if no slash is in the path. */
else
{
String slash = path.substring (0, pos);
int x = slash.length ();
if (x > 0)
/*
I don't know what you like to do here
*/
}

seeking a job as java-programmer in Berlin:
 
Sorry - forgot the code-tags:

a) backslash or slash?
b)
Code:
int pos = path.indexOf("/"));
if (pos == -1)
/* I don't know  what you like to do, if no slash is in the path. */
else
{
        String slash = path.substring (0, pos);
        int x = slash.length ();
        if (x > 0)
        /*
         I don't know  what you like to do here
        */
}

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top