Can this java function be optimized? public static String slugify(String text) { if…
- User
Can this java function be optimized?
public static String slugify(String text) {
codeif (text == null) { return null; } text = text.toLowerCase(); StringBuilder result = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (Character.isLetterOrDigit(c)) { result.append(c); } else { result.append('-'); } } // Remove double dashes while (result.indexOf("--") > -1) { result = new StringBuilder(result.toString().replace("--", "-")); } // Remove leading dashes while (result.length() > 0 && result.charAt(0) == '-') { result = new StringBuilder(result.substring(1)); } // Remove trailing dashes while (result.length() > 0 && result.charAt(result.length() - 1) == '-') { result = new StringBuilder(result.substring(0, result.length() - 1)); } return result.toString(); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.