String là final class trong Java và immutable, nghĩa là không thay đổi đối tượng nhưng có thể thay reference. Thẻ HTML loại bỏ khỏi chuỗi bằng replaceAll() của String. Dùng biểu thức chính quy (regular expression). Sau khi loại thẻ HTML, trả về chuỗi văn bản thường.
Cú pháp
public String replaceAll(String regex, String replacement)
Ví dụ
public class RemoveHTMLTagsTest {
public static void main(String[] args) {
String str = "Welcome to Tutorials Point
";
System.out.println("Before removing HTML Tags: " + str);
str = str.replaceAll("\\<.*?\\>", "");
System.out.println("After removing HTML Tags: " + str);
}
}
Kết quả
Before removing HTML Tags:Welcome to Tutorials Point
After removing HTML Tags: Welcome to Tutorials Point