String path = "/var/lib/////xen//images///rhel";
the number of slashes can be of any number. How to normalize the path in java like :
/var/lib/xen/images/rhel
                Use the built-in String method replaceAll, with a regular expression "/+", replacing one or more slashes with one slash:
path = path.replaceAll("/+", "/");
                        You could use a File object to output the path specific to the current platform:
String path = "/var/lib/////xen//images///rhel";
path = new File(path).getPath();
                        Use Guava's CharMatcher:
String simplifiedPath = CharMatcher.is('/').collapseFrom(originalPath, '/');
See: Guava Explained > Strings > CharMatcher
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With