Java String Slugify slugify(String input)

Here you can find the source of slugify(String input)

Description

Converts the string into a string containing only hyphens, lower-case letters, and numbers, removing all other characters.

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static String slugify(String input) 

Method Source Code

//package com.java2s;
/*/*w w w.  j av a2 s . c  o m*/
 * Stallion Core: A Modern Web Framework
 *
 * Copyright (C) 2015 - 2016 Stallion Software LLC.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version. This program is distributed in the hope that
 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 * License for more details. You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
 *
 *
 *
 */

import java.text.Normalizer;

import java.util.Locale;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern NONLATIN = Pattern.compile("[^\\w-]");
    private static final Pattern WHITESPACE = Pattern.compile("[\\s]");
    private static final Pattern MULTIHYPHENS = Pattern.compile("\\-\\-+");

    /**
     * Converts the string into a string containing only hyphens, lower-case letters, and numbers, removing all
     * other characters.
     *
     * @param input
     * @return
     */
    public static String slugify(String input) {
        String nowhitespace = WHITESPACE.matcher(input).replaceAll("-");
        String normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD);
        String slug = MULTIHYPHENS.matcher(NONLATIN.matcher(normalized).replaceAll("-")).replaceAll("-");
        return slug.toLowerCase(Locale.ENGLISH);
    }
}

Related

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