Java String Sanitize sanitize(String s)

Here you can find the source of sanitize(String s)

Description

Sanitizes a string so it can be used as an identifier

License

Apache License

Parameter

Parameter Description
s the string to sanitize

Return

the sanitized string

Declaration

public static String sanitize(String s) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**/*from  w  ww.  j a v a2  s .  c o  m*/
     * Sanitizes a string so it can be used as an identifier
     * @param s the string to sanitize
     * @return the sanitized string
     */
    public static String sanitize(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            switch (c) {
            case '\u00c0':
            case '\u00c1':
            case '\u00c3':
            case '\u00c4':
                sb.append('A');
                break;

            case '\u00c8':
            case '\u00c9':
            case '\u00cb':
                sb.append('E');
                break;

            case '\u00cc':
            case '\u00cd':
            case '\u00cf':
                sb.append('I');
                break;

            case '\u00d2':
            case '\u00d3':
            case '\u00d5':
            case '\u00d6':
                sb.append('O');
                break;

            case '\u00d9':
            case '\u00da':
            case '\u00dc':
                sb.append('U');
                break;

            case '\u00e0':
            case '\u00e1':
            case '\u00e3':
            case '\u00e4':
                sb.append('a');
                break;

            case '\u00e8':
            case '\u00e9':
            case '\u00eb':
                sb.append('e');
                break;

            case '\u00ec':
            case '\u00ed':
            case '\u00ef':
                sb.append('i');
                break;

            case '\u00f2':
            case '\u00f3':
            case '\u00f6':
            case '\u00f5':
                sb.append('o');
                break;

            case '\u00f9':
            case '\u00fa':
            case '\u00fc':
                sb.append('u');
                break;

            case '\u00d1':
                sb.append('N');
                break;

            case '\u00f1':
                sb.append('n');
                break;

            case '\u010c':
                sb.append('C');
                break;

            case '\u0160':
                sb.append('S');
                break;

            case '\u017d':
                sb.append('Z');
                break;

            case '\u010d':
                sb.append('c');
                break;

            case '\u0161':
                sb.append('s');
                break;

            case '\u017e':
                sb.append('z');
                break;

            case '\u00df':
                sb.append("ss");
                break;

            default:
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
                    sb.append(c);
                } else {
                    sb.append('_');
                }
                break;
            }
        }
        return sb.toString();
    }
}

Related

  1. sanitize(String methodHeader)
  2. sanitize(String mimeType)
  3. sanitize(String original)
  4. sanitize(String path)
  5. sanitize(String s)
  6. sanitize(String s)
  7. sanitize(String s, boolean allowColorCodes)
  8. sanitize(String source)
  9. sanitize(String str)