I’ve got this string which basically contains a conversation, and I want to “filter” out 1 person’s text with the Pattern class.
The conversation looks like this:
Jack: Hi
John: Hello
Jack: How are you?
John: I'm cool, how 'bout you?
Jack: I'm cool too.
I am trying to put every different line in a different string/different array.
So I wrote this method:
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;
}
The code results in:
String someteststring = "Jack: HiJack: How are you?Jack: I'm cool too."
However, I don’t want this, I want every found pattern to be put in a single different string, like this:
someteststring[0] = "Jack: Hi"
someteststring[1] = "Jack: How are you?"
someteststring[2] = "Jack: I'm cool too."
I hope this explains it, can anyone help?
1 Answer