Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At times you do not want to simply remove the characters, but just remove the accents. I came up with the following utility class which I use in my Java REST web projects whenever I need to include a String in an URL:</p> <pre><code>import java.text.Normalizer; import java.text.Normalizer.Form; import org.apache.commons.lang.StringUtils; /** * Utility class for String manipulation. * * @author Stefan Haberl */ public abstract class TextUtils { private static String[] searchList = { "Ä", "ä", "Ö", "ö", "Ü", "ü", "ß" }; private static String[] replaceList = { "Ae", "ae", "Oe", "oe", "Ue", "ue", "sz" }; /** * Normalizes a String by removing all accents to original 127 US-ASCII * characters. This method handles German umlauts and "sharp-s" correctly * * @param s * The String to normalize * @return The normalized String */ public static String normalize(String s) { if (s == null) return null; String n = null; n = StringUtils.replaceEachRepeatedly(s, searchList, replaceList); n = Normalizer.normalize(n, Form.NFD).replaceAll("[^\\p{ASCII}]", ""); return n; } /** * Returns a clean representation of a String which might be used safely * within an URL. Slugs are a more human friendly form of URL encoding a * String. * &lt;p&gt; * The method first normalizes a String, then converts it to lowercase and * removes ASCII characters, which might be problematic in URLs: * &lt;ul&gt; * &lt;li&gt;all whitespaces * &lt;li&gt;dots ('.') * &lt;li&gt;(semi-)colons (';' and ':') * &lt;li&gt;equals ('=') * &lt;li&gt;ampersands ('&amp;') * &lt;li&gt;slashes ('/') * &lt;li&gt;angle brackets ('&lt;' and '&gt;') * &lt;/ul&gt; * * @param s * The String to slugify * @return The slugified String * @see #normalize(String) */ public static String slugify(String s) { if (s == null) return null; String n = normalize(s); n = StringUtils.lowerCase(n); n = n.replaceAll("[\\s.:;&amp;=&lt;&gt;/]", ""); return n; } } </code></pre> <p>Being a German speaker I've included proper handling of German umlauts as well - the list should be easy to extend for other languages.</p> <p>HTH</p> <p><strong>EDIT:</strong> Note that it <em>may</em> be unsafe to include the returned String in an URL. You should at least HTML encode it to prevent XSS attacks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload