fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static String slugifyTitle(String value) {
  9. String slugifiedVal = null;
  10. if (value != null && !value.trim().isEmpty())
  11. slugifiedVal = value.toLowerCase()
  12. .replaceAll("[\\p{S}\\p{P}&&[^-]]+", "") // strips all special chars except -
  13. .replaceAll("[\\s-]+", "-") // converts spaces/hyphens to -
  14. .replaceAll("^-+|-+$", ""); // remove trailing/leading hyphens
  15. return slugifiedVal;
  16. }
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. List<String> strs = Arrays.asList("Heading with symbols *~!@#$%^&()_+-=[]{};',.<>?/",
  20. "Heading with an asterisk*",
  21. "Custom-id-&-stuff",
  22. "--Custom-id-&-stuff--");
  23. for (String str : strs)
  24. System.out.println("\"" + str + "\" => " + slugifyTitle(str));
  25. }
  26. }
Success #stdin #stdout 0.14s 50392KB
stdin
Standard input is empty
stdout
"Heading with symbols *~!@#$%^&()_+-=[]{};',.<>?/" => heading-with-symbols
"Heading with an asterisk*" => heading-with-an-asterisk
"Custom-id-&-stuff" => custom-id-stuff
"--Custom-id-&-stuff--" => custom-id-stuff