Here you can find the source of htmlEncode(String str)
Parameter | Description |
---|---|
str | the source string |
public static String htmlEncode(String str)
//package com.java2s; public class Main { /**//from w ww .j av a 2s. co m * Replaces the chars '>', '<', and '&' with the sequences '>', '<', and '&'. * @param str the source string * @return the encoded string */ public static String htmlEncode(String str) { StringBuilder result = null; for (int i = 0, max = str.length(), delta = 0; i < max; i++) { char c = str.charAt(i); String replacement = null; if (c == '&') { replacement = "&"; } else if (c == '<') { replacement = "<"; } else if (c == '>') { replacement = ">"; } if (replacement != null) { if (result == null) { result = new StringBuilder(str); } result.replace(i + delta, i + delta + 1, replacement); delta += (replacement.length() - 1); } } if (result == null) { return str; } return result.toString(); } }