Skip to content

Instantly share code, notes, and snippets.

@pyricau
Created May 20, 2012 09:37
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 pyricau/2757501 to your computer and use it in GitHub Desktop.
Save pyricau/2757501 to your computer and use it in GitHub Desktop.
Stripping html tags from a String
public class Strip {
public static void main(String[] args) {
// outputs "Hello world!"
System.out.println(stripHtmlTags("Hello <b>world</b><img src='somewhere'/>!"));
}
public static String stripHtmlTags(String input) {
StringBuilder sb = new StringBuilder(input.length());
boolean skipping = false;
for (char c : input.toCharArray()) {
if (skipping) {
if (c == '>') {
skipping = false;
}
} else {
if (c == '<') {
skipping = true;
} else {
sb.append(c);
}
}
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment