Java examples for java.lang:String HTML
decode Entities
/*/*from w w w . j a v a2 s.c om*/ You may freely copy, distribute, modify and use this class as long as the original author attribution remains intact. See message below. Copyright (C) 2004 Christian Pesch. All Rights Reserved. */ //package com.java2s; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] argv) { String str = "java2s.com"; System.out.println(decodeEntities(str)); } private static final Map<String, String> entities = new HashMap<String, String>(); public static String decodeEntities(String str) { StringBuilder builder = new StringBuilder(); int semicolonIndex = 0; while (semicolonIndex < str.length()) { int ampersandIndex = str.indexOf("&", semicolonIndex); if (ampersandIndex == -1) { builder.append(str.substring(semicolonIndex, str.length())); break; } builder.append(str.substring(semicolonIndex, ampersandIndex)); semicolonIndex = str.indexOf(";", ampersandIndex); if (semicolonIndex == -1) { builder.append(str.substring(ampersandIndex, str.length())); break; } String tok = str.substring(ampersandIndex + 1, semicolonIndex); if (tok.charAt(0) == '#') { tok = tok.substring(1); try { int radix = 10; if (tok.trim().charAt(0) == 'x') { radix = 16; tok = tok.substring(1, tok.length()); } builder.append((char) Integer.parseInt(tok, radix)); } catch (NumberFormatException exp) { builder.append('?'); } } else { tok = entities.get(tok); if (tok != null) builder.append(tok); else builder.append('?'); } semicolonIndex++; } return builder.toString(); } public static String trim(String string) { if (string == null) return null; StringBuffer buffer = new StringBuffer(string); for (int i = 0; i < buffer.length(); i++) { char c = buffer.charAt(i); if (Character.isWhitespace(c)) buffer.setCharAt(i, ' '); } return buffer.toString().trim(); } }