Java String Sanitize sanitize(String s)

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

Description

sanitize

License

Open Source License

Declaration

private static String sanitize(String s) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .  ja v  a2s.  c om*/
 * Help.java
 *
 * Copyright (C) 1998-2005 Peter Graves
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

public class Main {
    private static String sanitize(String s) {
        StringBuilder sb = null;
        final int limit = s.length();
        for (int i = 0; i < limit; i++) {
            char c = s.charAt(i);
            switch (c) {
            case '<':
                if (sb == null) {
                    sb = new StringBuilder();
                    sb.append(s.substring(0, i));
                }
                sb.append("&lt;");
                break;
            case '>':
                if (sb == null) {
                    sb = new StringBuilder();
                    sb.append(s.substring(0, i));
                }
                sb.append("&gt;");
                break;
            case '&':
                if (sb == null) {
                    sb = new StringBuilder();
                    sb.append(s.substring(0, i));
                }
                sb.append("&amp;");
                break;
            default:
                if (sb != null)
                    sb.append(c);
                break;
            }
        }
        return (sb != null) ? sb.toString() : s;
    }
}

Related

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