String (@NotNull ChooseByNameModel model, @NotNull String pattern) { final String[] separators = model.getSeparators(); int lastSeparatorOccurrence = 0; for (String separator : separators) { int idx = pattern.lastIndexOf(separator); if (idx == pattern.length() - 1) { // avoid empty name idx = pattern.lastIndexOf(separator, idx - 1); } lastSeparatorOccurrence = Math.max(lastSeparatorOccurrence, idx == -1 ? idx : idx + separator.length()); } return pattern.substring(lastSeparatorOccurrence); }