java中如何用双引号分割字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26294182/
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 String With double quotes in java
提问by kavie
This question is Pretty Simple
这个问题很简单
How to Split String With double quotes in java?,
java中如何用双引号分割字符串?
For example I am having string Do this at "2014-09-16 05:40:00.0",After Splitting, I want String like
例如,我有字符串 在 "2014-09-16 05:40:00.0" 执行此操作,拆分后,我想要字符串
Do this at
2014-09-16 05:40:00.0,
Any help how to achieve this?
任何帮助如何实现这一目标?
采纳答案by Ninad Pingale
This way you can escape inner double quotes.
这样您就可以转义内部双引号。
String str = "Do this at \"2014-09-16 05:40:00.0\"";
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
Output
输出
Do this at
2014-09-16 05:40:00.0
回答by Rayan Ral
Use method String.split()
It returns an array of String, splitted by the character you specified.
使用方法String.split()
它返回一个字符串数组,由您指定的字符分割。
回答by Ankur Singhal
public static void main(String[] args) {
String test = "Do this at \"2014-09-16 05:40:00.0\"";
String parts[] = test.split("\"");
String part0 = parts[0];
String part1 = parts[1];
System.out.println(part0);
System.out.println(part1);
}
output
输出
Do this at
2014-09-16 05:40:00.0
回答by Gurkan ?lleez
Try this code. Maybe it can help
试试这个代码。也许它可以帮助
String str = "\"2014-09-16 05:40:00.0\"";
String[] splitted = str.split("\"");
System.out.println(splitted[1]);
回答by ApproachingDarknessFish
The solutions provided thus far simply split the string based on any occurrence of double-quotes in the string. I offer a more advanced regex-based solution that splits only on the first double-quote that precedes a string of characters containedin double quotes:
迄今为止提供的解决方案只是根据字符串中出现的任何双引号来分割字符串。我提供了一个更高级的基于正则表达式的解决方案,它只在双引号中包含的字符串之前的第一个双引号上拆分:
String[] splitStrings =
"Do this at \"2014-09-16 05:40:00.0\"".split("(?=\"[^\"].*\")");
After this call, split[0]
contains "Do this at "
and split[1]
contains "\"2014-09-16 05:40:00.0\""
. I know you don't want the quotes around the second string, but they're easy to remove using substring
.
在此调用之后,split[0]
contains"Do this at "
和split[1]
contains "\"2014-09-16 05:40:00.0\""
。我知道您不想要第二个字符串周围的引号,但使用substring
.