Back to Java
2026-03-186 min read

Slug Generator (Java)

Learn Slug Generator (Java) step by step with clear examples and exercises.

Title: Slug Generator (Java) - A full guide for Web Developers

Why This Matters

In web development, creating unique and SEO-friendly URLs is crucial for a better user experience and improved search engine rankings. A slug generator helps in creating clean, readable, and short URLs from long and complex ones. In this lesson, we will learn how to create a slug generator using Java.

Importance of Slug Generator

A well-structured URL improves the user experience by making it easier for users to understand the content they are accessing. It also plays an essential role in search engine optimization (SEO) as search engines use URLs to index and rank web pages. A slug generator ensures that your website's URLs are clean, readable, and SEO-friendly.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the following:

  1. Java programming language (version 8 or higher)
  2. Data structures like arrays and strings
  3. Java I/O operations
  4. Basic knowledge of regular expressions
  5. Familiarity with exception handling
  6. Understanding of file input/output operations
  7. Knowledge about character encoding and decoding, especially for non-Latin characters (Chinese, Japanese, Korean - CJK)
  8. Experience with libraries like ICU4J for handling multilingual text

Core Concept

A slug generator is a simple program that converts a given string into a clean, URL-friendly version. The generated slug should be short, readable, and contain only lowercase letters, hyphens (-), underscores (_), and no spaces or punctuation.

In our Java implementation, we will use the following steps:

  1. Read the input string from user input or a file.
  2. Replace special characters, spaces, and punctuation with hyphens (-) or underscores (_).
  3. Remove duplicate hyphens and underscores.
  4. Convert all characters to lowercase.
  5. Trim leading and trailing hyphens and underscores.
  6. Ensure the slug is between 3 and 100 characters long.
  7. Return the generated slug as output.

Handling Special Characters for CJK Languages

For non-Latin languages like Chinese, Japanese, and Korean (CJK), we need to consider their unique character sets and encoding schemes. To handle these languages, you can use libraries like ICU4J for proper encoding and decoding of characters.

Worked Example

Let's create a simple SlugGenerator class with the mentioned steps:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
import com.ibm.icu.text.Normalizer;
import com.ibm.icu.text.RuleBasedCollator;

public class SlugGenerator {
private static final Pattern SPECIAL_CHARACTERS = Pattern.compile("[^a-zA-Z0-9\\s\\p{InCJKUnifiedIdeographs}\\p{InHangulSyllables}]+");

public static void main(String[] args) throws IOException {
// Read input from a file named "input.txt"
File inputFile = new File("input.txt");
Scanner scanner = new Scanner(inputFile);

while (scanner.hasNextLine()) {
String input = scanner.nextLine();
String slug = generateSlug(input);
System.out.println("Generated Slug: " + slug);
}
}

private static String generateSlug(String input) {
// Normalize the input for CJK characters
input = Normalizer.normalize(input, Normalizer.Form.NFD).replaceAll("[\\p{M}]", "");

// Replace special characters, spaces, and punctuation with hyphens (-) or underscores (_)
input = SPECIAL_CHARACTERS.matcher(input).replaceAll("-_");

// Remove duplicate hyphens and underscores
input = input.replaceAll("--", "-").replaceAll("__", "_");

// Convert all characters to lowercase
RuleBasedCollator collator = new RuleBasedCollator(RuleBasedCollator.PRIMARY, RuleBasedCollator.SECONDARY);
collator.setStrength(RuleBasedCollator.PRIMARY, RuleBasedCollator.IDENTICAL);
input = collator.getCollationKey(input).toLowerCase();

// Trim leading and trailing hyphens and underscores
input = input.trim().replaceFirst("^-", "").replaceFirst("-$", "");

// Ensure the slug is between 3 and 100 characters long
if (input.length() < 3 || input.length() > 100) {
throw new IllegalArgumentException("Slug length should be between 3 and 100 characters.");
}

return input;
}
}

In the above example, we first read strings from a file named "input.txt" using the Scanner class. Then, we call the generateSlug() method to convert each given string into a slug. The generateSlug() method performs the steps mentioned in the Core Concept section and handles CJK characters using ICU4J library.

Common Mistakes

  1. Forgetting to handle cases where the input string length is less than 3 or greater than 100 characters.
  2. Failing to remove duplicate hyphens and underscores from the generated slug.
  3. Not converting all characters to lowercase before generating the slug.
  4. Not trimming leading and trailing hyphens and underscores from the generated slug.
  5. Using incorrect regular expression patterns for replacing special characters, spaces, and punctuation with hyphens or underscores.
  6. Not handling exceptions properly when dealing with file input/output operations.
  7. Failing to normalize CJK characters before processing them in the slug generator.
  8. Not considering character encoding and decoding for non-Latin languages like Chinese, Japanese, and Korean (CJK).

Common Mistakes (Subheadings)

1.1 Handling Invalid Input Length

1.2 Removing Duplicate Hyphens and Underscores

1.3 Converting All Characters to Lowercase

1.4 Trimming Leading and Trailing Hyphens and Underscores

1.5 Using Correct Regular Expression Patterns

1.6 Proper Exception Handling

1.7 Normalizing CJK Characters

1.8 Handling Character Encoding for CJK Languages

Practice Questions

  1. Modify the SlugGenerator class to accept a filename as input instead of reading from a specific file. Assume that the file contains one string per line.
  2. Implement a method to validate the generated slug against a custom set of allowed characters (e.g., only alphanumeric characters and underscores).
  3. Create a test class for the SlugGenerator class with multiple test cases to ensure proper functioning of the generateSlug() method.
  4. Extend the SlugGenerator class to support additional character sets like Chinese, Japanese, and Korean (CJK) and generate slugs that are compatible with URL encoding for these languages.
  5. Implement a method that can handle multiple words in an input string by replacing spaces between words with hyphens or underscores while maintaining the correct order of words in the generated slug.
  6. Create a method to check if a given string is a valid slug according to the rules mentioned in this tutorial.

FAQ

  1. Why is it essential to remove duplicate hyphens and underscores from the slug?

Duplicate hyphens and underscores can cause issues in URL encoding and may lead to broken links or unexpected behavior in some web applications.

  1. Can I customize the character set used for generating slugs?

Yes, you can modify the regular expression pattern in the generateSlug() method to suit your specific requirements.

  1. What should I do if I want to allow underscores in addition to hyphens as part of the generated slug?

You can modify the regular expression pattern in the generateSlug() method to replace spaces, punctuation, and special characters with both hyphens (-) and underscores (_). Then, remove any duplicate underscores before trimming leading and trailing characters.

  1. How can I handle non-Latin characters like Chinese, Japanese, and Korean (CJK) in the slug generator?

To support CJK characters, you will need to use a different approach for replacing special characters, spaces, and punctuation with hyphens or underscores while ensuring proper encoding and decoding of these characters. You may want to consider using libraries like ICU4J for handling multilingual text.

  1. How can I generate slugs that are compatible with URL encoding for multiple words in an input string?

To handle multiple words in an input string, you can replace spaces between words with hyphens or underscores while maintaining the correct order of words in the generated slug. You may also want to consider using hyphenation dictionaries or libraries that provide hyphenation support for various languages.

  1. How can I check if a given string is a valid slug according to the rules mentioned in this tutorial?

You can create a method that checks if a given string contains only lowercase letters, hyphens (-), underscores (_), and no spaces or punctuation. Additionally, it should have a length between 3 and 100 characters.

Slug Generator (Java) | Java | XQA Learn