Slugify Titles With Normalizer

Turn arbitrary titles into URL-safe slugs: NFD normalization folds accented letters, then runs of non-alphanumerics collapse into single dashes.

JavaData Integrity

Code

snippet
import java.text.Normalizer;

public class Slugger {

    static String slugify(String title) {
        // NFD 拆出变音符后丢掉 combining marks:重音字母折成基础字母
        String folded = Normalizer.normalize(title, Normalizer.Form.NFD)
                .replaceAll("\\p{M}+", "");
        return folded.toLowerCase()
                .replaceAll("[^a-z0-9]+", "-")
                .replaceAll("^-+|-+$", "");
    }

    public static void main(String[] args) {
        for (String title : new String[] {
                "Café Con Leche: A Beginner's Guide",
                "  Hello   World -- 2026  ",
                "Crème Brûlée Édition #2"}) {
            System.out.println(slugify(title));
        }
    }
}

How it works

Count the class Slugger and the functions slugify and main as the entire surface area of Slugify Titles With Normalizer.

The file repeats itself only inside for (String title : new String[].

Points of care

  • No lockfile entry, no version pinning — the file needs nothing beyond the runtime.

Using it

The primary signature is static String slugify(String title)

Treat main as the orchestrator calling slugify in a fixed order.

Key statements

return folded.toLowerCase()
.replaceAll("[^a-z0-9]+", "-")
.replaceAll("^-+|-+$", "");

Interactive Tools for This Topic

FAQ

How do you run Slugify Titles With Normalizer?
It runs as-is — treat the file as the whole program and execute it.