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.
Code
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
SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Whitespace NormalizerTrim line edges, collapse repeated spaces, convert tabs, and remove blank lines from text without changing its words or punctuation.
FAQ
- How do you run Slugify Titles With Normalizer?
- It runs as-is — treat the file as the whole program and execute it.
Report a problem with this exampleOpens a pre-filled issue on GitHub.