
November 6th, 2012, 08:33 AM
|
|
|
|
Determining whether a user entered string is a relative or an absolute path
Hey guys.
I am writing a small program that needs to determine whether a user entered string is a relative path or an absolute path. Basically, what I am trying to do in pseudo-code is...
Code:
private static boolean isRelative(String path) {
//return true if the path is relative, false if it is absolute
}
...
String path = getUserEnteredPath();
if ( isRelative(path) ) {
//append an environment variable to the path before we use it
} else {
//utilize the path in its current form
}
...
The part I am struggling with is the best way to write the isRelative method. I was thinking about using a regular expression on the first three characters against "[A-Z]:\", but there are a few things I don't like about that approach. For one, it will only work on Windows, which is acceptable but not ideal. Second, I don't know how to use regular expressions and would just as soon avoid doing so if I can get by without it.
What do you guys think, is there a better way?
|