sin*_*pop 3 java string quotation-marks
Java中是否存在用于拆分字符串的默认/简单方法,但是要使用引号或其他符号?
例如,给定此文本:
There's "a man" that live next door 'in my neighborhood', "and he gets me down..."
Run Code Online (Sandbox Code Playgroud)
获得:
There's
a man
that
live
next
door
in my neighborhood
and he gets me down
Run Code Online (Sandbox Code Playgroud)
这样的东西适用于您的输入:
String text = "There's \"a man\" that live next door "
+ "'in my neighborhood', \"and he gets me down...\"";
Scanner sc = new Scanner(text);
Pattern pattern = Pattern.compile(
"\"[^\"]*\"" +
"|'[^']*'" +
"|[A-Za-z']+"
);
String token;
while ((token = sc.findInLine(pattern)) != null) {
System.out.println("[" + token + "]");
}
Run Code Online (Sandbox Code Playgroud)
以上打印(如ideone.com上所示):
[There's]
["a man"]
[that]
[live]
[next]
[door]
['in my neighborhood']
["and he gets me down..."]
Run Code Online (Sandbox Code Playgroud)
它使用Scanner.findInLine,正则表达式模式是以下之一:
"[^"]*" # double quoted token
'[^']*' # single quoted token
[A-Za-z']+ # everything else
Run Code Online (Sandbox Code Playgroud)
毫无疑问,这并不总是100%有效; 引号可以嵌套等的情况将是棘手的.
| 归档时间: |
|
| 查看次数: |
2360 次 |
| 最近记录: |