我有这个基本上包含对话的字符串,我想用Pattern类“过滤”1个人的文本。
对话如下:
Jack: Hi
John: Hello
Jack: How are you?
John: I'm cool, how 'bout you?
Jack: I'm cool too.
我正在尝试将每个不同的行放在不同的字符串/不同的数组中。
所以我写了这个方法:
private String getFrom(String in, String type) {
String patr = "", Return = "";
if (type == "title") {
patr = "Jack:";
}
Pattern patr = Pattern.compile(patr);
Matcher matcher = pattern.matcher(in);
while (matcher.find()) {
Return = main.substring(matcher.start(), matcher.end());
}
if (type == "title") {
Return = Return.substring(0, Return.length());
}
return Return;
}
代码结果为:
String someteststring = "Jack: HiJack: How are you?Jack: I'm cool too."
但是,我不希望这样,我希望每个找到的模式都放在一个不同的字符串中,如下所示:
someteststring[0] = "Jack: Hi"
someteststring[1] = "Jack: How are you?"
someteststring[2] = "Jack: I'm cool too."
我希望这可以解释它,任何人都可以帮忙吗?
答案 0 :(得分:1)
String[] finalText = somteststring.split("\n");