Java String Quote quoteCharacterData(char[] ch, int start, int length)

Here you can find the source of quoteCharacterData(char[] ch, int start, int length)

Description

Replace critical characters by XML entities.

License

Open Source License

Declaration

static String quoteCharacterData(char[] ch, int start, int length) 

Method Source Code

//package com.java2s;

public class Main {
    /** Replace critical characters by XML entities. */
    static String quoteCharacterData(char[] ch, int start, int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = start; i < start + length; i++) {
            char c;
            switch (c = ch[i]) {
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            default:
                sb.append(c);// w ww  .  ja v a 2s .c o  m
                break;
            }
        }
        return sb.toString();
    }

    /** Replace critical characters by XML entities. */
    static String quoteCharacterData(String s) {
        return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }
}

Related

  1. quoteAtom(String term)
  2. quoteAtom(String term)
  3. quoteAttrValue(String s)
  4. quoteCanonical(String s)
  5. quoteCeylonKeywords(String qualifiedName)
  6. quoteCharacterData(String s)
  7. quoteCharacters(String s)
  8. quoteCharCode(int code)
  9. quoteCharPresent(String quote)