Skip to content

Instantly share code, notes, and snippets.

@flaudanum
Created March 11, 2020 14:42
Show Gist options
  • Save flaudanum/7c8bbece77406c9c48f91f7b799e079d to your computer and use it in GitHub Desktop.
Save flaudanum/7c8bbece77406c9c48f91f7b799e079d to your computer and use it in GitHub Desktop.
Remove ending backslash and trailing space from a Windows path
String winPath = "C:\\Users\\f.laudanum\\Documents\\My-Directory\\ ";
String cleanPath;
/* REGEXP pattern: '\\\s*' with doubled backslashes */
Pattern pattern = Pattern.compile("\\\\\\s*$");
Matcher match = pattern.matcher(winPath);
/* Call to find(0) triggers the pattern search from text's start*/
if (match.find(0)) {
cleanPath = match.replaceAll("");
} else {
cleanPath = winPath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment