[英]How to escape ],[ sequence in java?
While writing code in java, I need to split string with "],[" . 在用Java编写代码时,我需要用“],[”分割字符串。 Below is my code. 下面是我的代码。
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
int i=0;
String line;
line = reader.readLine();
String[] split = line.split("],[");
StringBuilder sb = new StringBuilder();
sb.append(split[0]);
String joined = sb.toString();
System.out.println(joined);
}
Please help here. 请在这里帮助。
You have to escape the [
. 您必须转义[
。 Like: 喜欢:
line.split("\\],\\[");
Your code would be: 您的代码为:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
int i=0;
String line;
line = reader.readLine();
String[] split = line.split("\\],\\[");
StringBuilder sb = new StringBuilder();
sb.append(split[0]);
String joined = sb.toString();
System.out.println(joined);
}
Escape the opening square bracket because [
is a special meta character in regex represents the start of a character class. 逃避方括号,因为[
是正则表达式中的特殊元字符,代表字符类的开始。
String[] split = line.split("],\\[");
]
will match a literal ]
symbol when there is no start of the character class symbol [
exists before ]
. 当字符类符号[
在]
之前没有开始时, ]
将与文字]
符号匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.