Java Char Array to Entity charsToEntities(String str)

Here you can find the source of charsToEntities(String str)

Description

Converts <, >, & in the string to their HTML entity equivalents.

License

Open Source License

Parameter

Parameter Description
str The string

Declaration

public static String charsToEntities(String str) 

Method Source Code

//package com.java2s;
/**/*  w ww  .  ja v  a  2 s.c  o m*/
 * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and
 * www.integratedmodelling.org. 
    
   This file is part of Thinklab.
    
   Thinklab 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 3 of the License,
   or (at your option) any later version.
    
   Thinklab 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 Thinklab.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts &lt;, &gt;, &amp; in the string to their HTML entity
     * equivalents.
     * @param str The string
     * @since jEdit 4.2pre1
     */
    public static String charsToEntities(String str) {
        StringBuffer buf = new StringBuffer(str.length());
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            switch (ch) {
            case '<':
                buf.append("&lt;");
                break;
            case '>':
                buf.append("&gt;");
                break;
            case '&':
                buf.append("&amp;");
                break;
            default:
                buf.append(ch);
                break;
            }
        }
        return buf.toString();
    }
}

Related

  1. charsToEntities(String str, boolean xml11)