First of all you can not have a string as you posted in question
String fname="C:\textfiles\db\query\query.txt";
this should be replaced by
String fname="C:\\textfiles\\db\\query\\query.txt";
as backslash("\") needs an escape as well.
Finally you need to do something like this to split them:
String fname="C:\\textfiles\\db\\query\\query.txt";
String[] items= fname.split("\\\\");
System.out.println(Arrays.toString(items));
Hope this helps.
Answer from Sanjeev on Stack OverflowFirst of all you can not have a string as you posted in question
String fname="C:\textfiles\db\query\query.txt";
this should be replaced by
String fname="C:\\textfiles\\db\\query\\query.txt";
as backslash("\") needs an escape as well.
Finally you need to do something like this to split them:
String fname="C:\\textfiles\\db\\query\\query.txt";
String[] items= fname.split("\\\\");
System.out.println(Arrays.toString(items));
Hope this helps.
'split' expects RegEx. Best way to use split is using "Pattern.quote"
String separator = "\\";
String value = "C:\\Main\\text.txt";
String[] arrValues = value.split(Pattern.quote(separator));
Just to add on
\ is a special character in regular expressions, as well as in Java string literals. If you want a literal backslash in a regex, you have to double it twice.
When you type "\\", this is actually a single backslash (due to escaping special characters in Java Strings).
Regular expressions also use backslash as special character, and you need to escape it with another backslash. So in the end, you need to pass "\\" as pattern to match a single backslash.
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String taskArray[] = taskOwner.split("\\\\");
System.out.println(taskArray[0]);
}
output
test
00216243
\002 represents a unicode character. SO i suggest you to split your input according to the character other than alphabet or digit.
String string1 = "test\00216243";
String part[] = string1.split("[^a-z0-9]");
System.out.println(part[0]);
Output:
test
Oh no... please don't start messing with Windows filenames. One thing you don't want is to have platform-dependent code. Rather than this, use standard Java library:
System.out.println(new File("C:\\Users\\Owner\\Desktop\\foo.txt").getName());
Finally, if you really had to parse the path manually, I would use File.separatorChar to make the code portable.
// hardcoded here for the example, but you would actually get it from somewhere
String path = "C:\\Users\\Owner\\Desktop\\foo.txt";
int i = path.lastIndexOf(File.separatorChar);
String last = i < 0 || i == s.length() ? "" : path.substring(i + 1);
System.out.println(last);
This is also less expensive than splitting a string since you're only interested in the last element.
The backslash is reserved, so you have to use a double-backslash like so:
filename.split("\\\\")
To make this solution work consistently across platforms, however, it would be better to use:
filename.split(Pattern.quote(File.separator))
Alternatively, as Dici pointed out, you could just do:
new File(filename).getName()
In regex-land, a \ is an escape character, so to obtain a literal \ we need to escape it: \\. However, in Java strings, \ is also an escape character, so we need to escape each \ a second time, resulting in \\\\. Therefore, this is what you want:
str.split("\\\\")
The regex would be "\\\\"
In the source code: "\\\\"
After compiling, the string is: "\\"
And the regex parser interperets this as look for a \, So it matches one backslash '\'
Don't split. Just parse:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy\\MM\\dd");
LocalDate dPresent = LocalDate.parse(datum, formatter);
Use this method...
String date = "21/11/2016";
String[] splitedDataFrmDate = date.split("\\/");
for(String data:splitedDataFrmDate)
{
System.out.println(data);
}
My guess is that you are missing that backslash ('\') characters are escape characters in Java String literals. So when you want to use a '\' escape in a regex written as a Java String you need to escape it; e.g.
Pattern.compile("\."); // Java syntax error
// A regex that matches a (any) character
Pattern.compile(".");
// A regex that matches a literal '.' character
Pattern.compile("\\.");
// A regex that matches a literal '\' followed by one character
Pattern.compile("\\\\.");
The String.split(String separatorRegex) method splits a String into substrings separated by substrings matching the regex. So str.split("\\.") will split str into substrings separated by a single literal '.' character.
The regex "." would match any character as you state. However an escaped dot "\." would match literal dot characters. Thus 192.168.1.1 split on "\." would result in {"192", "168", "1", "1"}.
Your wording isn't completely clear, but I think this is what you're asking.