Reputation: 970
I tried splitting like this-
tableData.split("\\"")
but it does not work.
Upvotes: 2
Views: 23541
Reputation: 457
You can actually split without the backward slash. You only have to use single quote
tableData.split('"');
Upvotes: 0
Reputation: 3510
You have escaped the \
by putting in \
twice, try
tableData.split("\"")
Why does this happen?
A backslash escapes the following character. Since the next character is another backslash, the second backslash will be escaped, thus the doublequote won't.
Your resulting escaped string is \"
, where it should really be just "
.
Edit:
Also keep in mind, that String.split()
interprets its pattern parameter as a regular expression, which has several special characters, which have to be escaped in the resulting string.
So if you want split by a .
(which is a special regex character), you need to specify it as String.split("\\.")
. The first backslash escapes the escaping function of the second backlash and would result in "\."
.
In case of regex characters you could also just use Pattern.quote();
to escape your desired delimiter, but this is far out of the scope the question orignally had.
Upvotes: 4
Reputation: 124215
It seems that you tried to escape it same way as you would escape |
which is "\\|"
. But difference between |
and "
is that
|
is metacharacter in regex engine (it represents OR operator)"
is metacharacter in Java language in string literal (it represents start/end of the string)To escape any String metacharacter (like "
) you need to place before it other String metacharacter responsible for escaping which is \
1. So to create String which would contain "
like this is "quote"
you would need to write it as
String s = "this is \"quote\"";
// ^^ ^^ these represent " literal, not end of string
Same idea is applied if we would like to create \
literal (we would need to escape it by placing another \
before it). For instance if we would want to create string representing c:\foo\bar
we would need to write it as
String s = "c:\\foo\\bar";
// ^^ ^^ these will represent \ literal
So as you see \
is used to escape metacharacters (make them simple literals).
This character is used in Java language for Strings, but it also is used in regex engine to escape its metacharacters:
\
, ^
, $
, .
, |
, ?
, *
, +
, (
, )
, [
, {
.
If you would like to create regex which will match [
character you will need to use regex \[
but String representing this regex in Java needs to be written as
String leftBracketRegex = "\\[";
// ^^ - Remember what was said earlier?
// To create \ literal in String we need to escape it
So to split on [
we would need to invoke split("\\[")
because regex representing [
is \[
which needs to be written as "\\["
in Java.
Since "
is not special character in regex but it is special in String we need to escape it only in string literal by writing it as
split("\"");
1) \
is also used to create other characters line separators \n
, tab \t
. It can also be used to create Unicode characters like \uXXXX
where XXXX
is index of character in Unicode table in hexadecimal form.
Upvotes: 7
Reputation: 929
A single backslash will do the trick.
Like this:
tableData.split("\"");
Upvotes: 0
Reputation: 365
You are not escaping properly. The snippet code will not even compile because of it. The correct way to do it is
tableData.split("\"");
Upvotes: 0
Reputation: 172418
Try like this by escaping "
with single backslash \
:
tableData.split("\"")
Upvotes: 1