String (@NotNull String name, boolean strict, @NotNull String replacement) { StringBuilder result = null; int last = 0; int length = name.length(); for (int i = 0; i < length; i++) { char c = name.charAt(i); boolean appendReplacement = true; if (c > 0 && c < 255) { if (strict ? Character.isLetterOrDigit(c) || c == '_' : Character.isJavaIdentifierPart(c) || c == ' ' || c == '@' || c == '-') { continue; } } else { appendReplacement = false; } if (result == null) { result = new StringBuilder(); } if (last < i) { result.append(name, last, i); } if (appendReplacement) { result.append(replacement); } last = i + 1; } if (result == null) { return name; } if (last < length) { result.append(name, last, length); } return result.toString(); }