Post

Java split backslash

Java split backslash

This one burned me badly the first time. Java’s split() method takes a regex, not a plain string — and backslash is a special character in both Java strings AND regex. So to match a single backslash, you need four of them in your Java code. Looks insane, but once you understand why, you’ll never forget it.

1
2
3
String folders="E:\\folder1\\folder2\\folder3\\folder4";
String[] folder= folders.split("\\\\");
System.out.println(java.util.Arrays.toString(folder));
This post is licensed under CC BY 4.0 by the author.