gpt4 book ai didi

java - 如何在java中转义],[序列?

转载 作者:行者123 更新时间:2023-12-01 18:16:17 30 4
gpt4 key购买 nike

在用java编写代码时,我需要用“],[”分割字符串。下面是我的代码。

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);

}

请在这里帮忙。

最佳答案

你必须转义[。喜欢:

line.split("\\],\\[");

您的代码将是:

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);

}

关于java - 如何在java中转义],[序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29293479/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com