Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jtryan
Created October 18, 2016 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtryan/1c86c66e9b5f5f1f4cc6cddd62b4fe09 to your computer and use it in GitHub Desktop.
Save jtryan/1c86c66e9b5f5f1f4cc6cddd62b4fe09 to your computer and use it in GitHub Desktop.
// Return HTML Entity code equivalents for any special characters
public static String HTMLEntityEncode( String input ) {
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < input.length(); ++i ) {
char ch = input.charAt( i );
if ( ch>='a' && ch<='z' || ch>='A' && ch<='Z' || ch>='0' && ch<='9' ) {
sb.append( ch );
} else {
sb.append( "&#" + (int)ch + ";" );
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment