static private String printTextPerRole(String[] roles, String[] textLines) { StringBuilder output = new StringBuilder(""); for (String role : roles) { output.append(role + ":\n"); for (int i = 0; i < textLines.length; i++) { if (textLines[i].startsWith(role & ": ")) { output.append(i + 1 + ") "); output.append(textLines[i].substring(role.length() + 2)); output.append("\n"); } } output.append("\n"); } return output.toString(); } How can I change the string to find the necessary characters if (textLines [i] .startsWith (role)) Since the string role (in the array are {Urbanity, Ammos Fedorovich}), there are lines without:, then I add ": \ n ". But since there are textLines lines in the second parameter — the same words are found, but already with double :: or a hyphen — the desired parameter cannot be identified. How to deal with this? Those. How to make the search more universal? (I hope I correctly expressed my thought)