public static String printTextPerRole(String[] roles, String[] textLines) {
StringBuilder textPerRole = new StringBuilder();
Код: Выделить всё
for (String role : roles) {
StringBuilder text = new StringBuilder();
textPerRole.append(role).append(":").append("\n");
for (int i = 0; i < textLines.length; i++) {
if (textLines[i].startsWith(role + ":")) {
textLines[i] = textLines[i].replaceFirst(role + ":", "");
text.append(i + 1).append(")").append(textLines[i]).append("\n");
}
}
if (text.length() > 0) {
textPerRole.append(text);
}
textPerRole.append("\n");
}
return textPerRole.toString();
Подробнее здесь: https://stackoverflow.com/questions/791 ... d0%b0-java