Java String Sanitize sanitize(CharSequence string)

Here you can find the source of sanitize(CharSequence string)

Description

Clean strings from illegal XML 1.0 characters.

License

Apache License

Parameter

Parameter Description
string string to clean

Return

the cleaned string

Declaration

public static String sanitize(CharSequence string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w  w w  .j  a  va  2  s .  co m*/
     * Clean strings from illegal XML 1.0 characters.
     * See <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets">XML charset</a>
     *
     * @param string string to clean
     * @return the cleaned string
     */
    public static String sanitize(CharSequence string) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, len = string.length(); i < len; i++) {
            char c = string.charAt(i);
            boolean legal = c == '\u0009' || c == '\n' || c == '\r' || (c >= '\u0020' && c <= '\uD7FF')
                    || (c >= '\uE000' && c <= '\uFFFD');
            if (legal) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Related

  1. sanitize(char orig)
  2. sanitize(double[] outdata)
  3. sanitize(final String main)
  4. sanitize(final String name)
  5. sanitize(final String s)