Java String Slugify toSlug(final String input)

Here you can find the source of toSlug(final String input)

Description

Convert the String input to a slug.

License

Open Source License

Parameter

Parameter Description
input The string to be converted to a Slug

Return

Slug-String representation of the given input string

Declaration

public static String toSlug(final String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.Locale;
import java.util.regex.Pattern;

public class Main {
    /**//from  w w w. ja  va 2  s. co  m
     * Convert the String input to a slug.
     * 
     * @param input The string to be converted to a Slug
     * @return Slug-String representation of the given input string
     */
    public static String toSlug(final String input) {
        if (input == null) {
            throw new IllegalArgumentException("Input cannot be null");
        }

        final Pattern NONLATIN = Pattern.compile("[^\\w-]");
        final Pattern WHITESPACE = Pattern.compile("[\\s]");

        String nowhitespace = WHITESPACE.matcher(input).replaceAll("-");
        String normalized = Normalizer.normalize(nowhitespace, Form.NFD);
        String slug = NONLATIN.matcher(normalized).replaceAll("");
        return slug.toLowerCase(Locale.ENGLISH);
    }
}

Related

  1. slugify(final String s)
  2. slugify(final String s)
  3. slugify(String input)
  4. slugify(String input)
  5. slugify(String s)
  6. toSlug(String input)
  7. toSlug(String input)
  8. toSlug(String s)