1

I have several strings that are being combined to make a URI. I want to ensure that the resulting URI is as simple as possible. For example, given

str1 = "/dir1/dir2";
str2 = "./dir3/";

I want to end up with "/dir1/dir2/dir3/" not "/dir1/dir2/./dir3/".

Is there a Java library that performs this kind of simplification?

4 Answers 4

4

try the class java.net.URI

  URI uri = new URI("http://localhost/");      
  System.out.println(uri.resolve("dir1/dir2/./dir3/"));
1

JDK7 has a Path.normalize method which removes redundant name elements from a path. It also contains Path.resolve which can be used to join two paths.

Take a look at the Java Tutorial | Path Operations for more information.

0

I guess URI can don that kind of job, no ?

0

If the URI class doesn't do what you want, there is more flexibility and power in the UriBuilder class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.