我有一個字符串,我想去除所有標籤與<和>從java字符串中刪除所有 「<...>」
例如:
在String<title>Java Code</title>
將
Java Code
和
<pre><font size="7"><strong>Some text here
</strong></font><strong>
將
Some text here
怎樣才使用的charAt(我)做什麼? 在此先感謝
我有一個字符串,我想去除所有標籤與<和>從java字符串中刪除所有 「<...>」
例如:
在String<title>Java Code</title>
將
Java Code
和
<pre><font size="7"><strong>Some text here
</strong></font><strong>
將
Some text here
怎樣才使用的charAt(我)做什麼? 在此先感謝
怎樣才使用的charAt(我)做什麼?
方法如下:
public static void main(String[] args) {
String s = "<pre><font size=\"7\"><strong>Some text here\n\n</strong></font><strong>";
String o = "";
boolean append = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '<')
append = false;
if (append)
o += s.charAt(i);
if (s.charAt(i) == '>')
append = true;
}
System.out.println(o);
}
使用正則表達式執行此操作非常簡單。
String src = "<title>Java Code</title>";
String dst = src.replaceAll("<.+?>", "");
System.out.println(dst);
他想用'charAt'。 :) – dacwe 2012-03-03 11:58:29
與charAt
,你可以循環遍歷字符串你所有的字符,刪除一切從<,直到下一個>。但是,您的字符串可能包含非ASCII的UTF代碼點,這可能會破壞此方法。
我會用正則表達式去,像
String someTextHere = "...";
String cleanedText = someTextHere.replaceAll("<[^>]*?>", "");
然而,還讓我點你this question,其中列出了與正則表達式的方法的關注。
既然你特別希望使用chatAt(I),這裏是算法,
你不想使用正則表達式有什麼特別的原因嗎? – 2012-03-03 11:56:41
@AnotherCode我有文字也充滿< >標籤,我想刪除它們,只看到文字。 – Mustafa 2012-03-03 11:59:09
你可以找到類似的職位,它會幫助你, http://stackoverflow.com/questions/240546/removing-html-from-a-java-string – Vinesh 2012-03-03 11:59:34