scala 如何用双引号“作为分隔符分割字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29986357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a string with double quotes " as the delimiter?
提问by Goku__
I tried splitting like this-
我试着像这样分裂——
tableData.split("\"")
but it does not work.
但它不起作用。
回答by Pshemo
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)
|是正则表达式引擎中的元字符(它代表 OR 运算符)"是字符串文字中 Java 语言中的元字符(它代表字符串的开始/结束)
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 元字符(如"),您需要在它之前放置其他负责转义的 String 元字符,即\1。因此,要创建的字符串,其中将包括"像this is "quote"你需要把它写成
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\barwe would need to write it as
如果我们想创建\文字(我们需要通过\在它之前放置另一个来逃避它),同样的想法也适用。例如,如果我们想创建字符串表示,c:\foo\bar我们需要将其写为
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:\, ^, $, ., |, ?, *, +, (, ), [, {.
因此,如您所见\,用于转义元字符(使它们成为简单的文字)。
此字符在 Java 语言中用于字符串,但它也用于正则表达式引擎以转义其元字符:\, ^, $, ., |, ?, *, +, (, ), [, {。
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
如果您想创建匹配[字符的正则表达式,您将需要使用正则表达式,\[但在 Java 中表示此正则表达式的字符串需要写为
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.
所以拆就[我们就需要调用split("\\["),因为正则表达式表示[就是\[需要被写成"\\["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 \uXXXXwhere XXXXis index of character in Unicode table in hexadecimal form.
1)\也用于创建其他字符的行分隔符\n、制表符\t。它还可以用于创建 Unicode 字符,例如十六进制形式的 Unicode 表中字符的索引在\uXXXX哪里XXXX。
回答by Zhedar
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 "\.".
编辑:
还要记住,它将String.split()其模式参数解释为一个正则表达式,它有几个特殊字符,必须在结果字符串中转义。
因此,如果您想按 a .(这是一个特殊的正则表达式字符)拆分,则需要将其指定为String.split("\\."). 第一个反斜杠转义了第二个反斜杠的转义函数,将导致"\.".
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.
对于正则表达式字符,您也可以使用Pattern.quote();来转义所需的分隔符,但这远远超出了问题最初的范围。
回答by Masudul
Try with single backslash \
尝试使用单个反斜杠 \
tableData.split("\"")
回答by Rahul Tripathi
Try like this by escaping "with single backslash \:
通过"使用单个反斜杠转义来尝试这样做\:
tableData.split("\"")
回答by koolkoda
You are not escaping properly. The snippet code will not even compile because of it. The correct way to do it is
你没有正确逃脱。因此,片段代码甚至无法编译。正确的做法是
tableData.split("\"");
回答by Manisha Singh Sanoo
A single backslash will do the trick.
Like this:
一个反斜杠就可以解决问题。
像这样:
tableData.split("\"");

