當前位置:首頁 » 編程語言 » java去標簽

java去標簽

發布時間: 2022-04-26 11:45:22

❶ 跪求java去除HTML標簽的代碼!

如果距離雲南旅遊指南最近的一對標簽是確定的話
先找到<spanstyle 然後獲取從它開始的第一個>

然後再找距離它最近的<

然後substing就解決了

❷ java 如何去除html中的一個指定標簽和指定標簽里的內容

java處理html指定標簽最好用正則表達式。例如要去除html中所有的h1標簽和類容就可以用下面的演示代碼:

packagekonw.reg;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassRemoveTag
{
publicstaticvoidmain(String[]args)
{
FileReaderfr;
Stringcontent=null;
Stringregex="<[Hh]1>.*</[Hh]1>";
try
{
fr=newFileReader("tag.html");
BufferedReaderbr=newBufferedReader(fr);
Stringstr=null;
StringBuffersb=newStringBuffer();
while((str=br.readLine())!=null)
{
sb.append(str+" ");
}
content=sb.toString();
br.close();

}catch(FileNotFoundExceptione)
{
e.printStackTrace();
}catch(IOExceptione)
{
e.printStackTrace();
}
Patternpattern=Pattern.compile(regex);
Matchermatcher=pattern.matcher(content);
StringBuffersb1=newStringBuffer();
while(matcher.find())
{
sb1.append(matcher.replaceAll("")+" ");
}
try
{
FileWriterfw=newFileWriter("tag.html");
BufferedWriterbw=newBufferedWriter(fw);
fw.write(sb1.toString());
bw.close();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}

❸ java如何去掉字元串中的 html標簽

1.去除單個HTML標記
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML標記
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLSpirit{ ITjob 遠標教育
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定義script的正則表達式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定義style的正則表達式
String regEx_html="<[^>]+>"; //定義HTML標簽的正則表達式

Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //過濾script標簽

Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //過濾style標簽

Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //過濾html標簽

return htmlStr.trim(); //返迴文本字元串
}
}

❹ 用java字元串方法去除HTML代碼標簽的問題

可以通過replaceAll方法進行字元串替換,之後替換的內容用正則表達式來匹配。舉例

String ss="<div id='mini_nav_qq'><li><a target='_top' " +

"href='http:// lady.qq.com/emo/emotio.shtml'>情感</a></li><li>" +

"<a target='_top' href='http://lady.qq.com/beauty/beauty.shtml'>美容</a></li></div>";

String ss=ss.replaceAll("<(/?\S+)\s*?[^<]*?(/?)>","<$1$2>");//通過只保留"<「後面的字元串,之後刪除空格和後面的內容,快捷的實現去除操作(此方法通用於所有的標簽去除,只需要傳入不同的ss值)。

結果就是:<div><li><a>情感</a></li><li><a>美容</a></li></div>。

❺ JAVA怎樣去銷毀一個標簽

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author daniel
*/
public class Demo extends JFrame {
private int count = 0;
Timer timer;

public Demo() {
setLayout(null);

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {
timer = new Timer(1000, new TimerListener());
timer.start();
}

public void mouseReleased(MouseEvent e) {
timer.stop();
{
getContentPane().removeAll();
System.gc();
}
getContentPane().repaint();
}
});

// 創建窗體
this.setTitle("按下滑鼠不動信息交替出現..松開消失");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
{
getContentPane().removeAll();
System.gc();
}
getContentPane().repaint();

if ((count % 2) == 0) {
JLabel lal_1 = new JLabel("JAVA is fun");
lal_1.setSize(100, 100);
getContentPane().add(lal_1);
lal_1.setLocation(100, 100);
lal_1.setVisible(true);
} else {
JLabel lal_2 = new JLabel("JAVA is powerful");
lal_2.setSize(100, 100);
getContentPane().add(lal_2);
lal_2.setLocation(250, 100);
lal_2.setVisible(true);
}
count += 1;
}
}
public static void main(String[] args) {
Demo d = new Demo();
}
}

java的特點是「永遠不需銷毀對象」,也似乎沒有手動銷毀對象的機制。它有一個垃圾回收站,當一個對象出了作用域,且引用數為0時,就可能被垃圾回收。System.gc()用於強制進行終結動作,可加速清理。

為了能夠及時清理某個對象,就將它的作用域設置得盡量小。將對象作為類成員的話,作用域太大,要等類銷毀時才有機會被回收。

如果覺得getContentPane().removeAll()會同時remove掉其它對象的話,將lal_1和lal_2用一個小一點的容器裝起來。

❻ java去除文本內容的標簽跟 

這個方法是替換內容,最後trim方法是,清除字元串兩邊的空格
String newStr =str.replaceAll("<p>", "").replaceAll("</p>", "").trim();

❼ java去掉欄位中的html標簽

用正則表達式吧,應該比較簡單。
或者使用笨點的方法,循環查找'>'符號的位置,判斷下一個字元是不是'<',如果是,則繼續循環,如果不是則是需要留下的文本了,把文本用list保存起來繼續循環直到全部欄位結束。
最後list裡面就是你要留下的文本了

❽ 用java去除掉這段代碼的HTML標簽

public static String HtmlText(String inputString) {
String htmlStr = inputString; //含html標簽的字元串
String textStr ="";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定義script的正則表達式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定義style的正則表達式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
String regEx_html = "<[^>]+>"; //定義HTML標簽的正則表達式

p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); //過濾script標簽

p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); //過濾style標簽

p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); //過濾html標簽

/* 空格 —— */
// p_html = Pattern.compile("\\ ", Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = htmlStr.replaceAll(""," ");

textStr = htmlStr;

}catch(Exception e) {
}
return textStr;
}

傳你的字元串進去看看,可以的話加分,謝謝

❾ 在java圖形界面中如何刪除已定義的標簽

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

public class LabelDelTest extends JFrame implements ActionListener {
JLabel label;
JButton button;
public LabelDelTest() {
label = new JLabel("新建標簽",JLabel.CENTER);
label.setBorder(new TitledBorder(""));
button = new JButton("刪除標簽");
button.addActionListener(this);
add(label,"North");
add(button,"South");
setTitle("標簽刪除");
setBounds(200,100,500,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {

new LabelDelTest();
}

public void actionPerformed(ActionEvent e) {
remove(label);
this.repaint();
}

}

❿ java 利用jsoup 如何去除一段代碼中的所有html標簽,只留純文本

這是文檔,很簡單的看看就懂了

網頁鏈接

熱點內容
煙花的代碼c語言 發布:2025-07-01 23:56:04 瀏覽:223
安卓默認打開文件方式怎麼修改 發布:2025-07-01 23:30:38 瀏覽:862
壓縮機接線座 發布:2025-07-01 23:17:48 瀏覽:662
iqoo瀏覽器緩存路徑 發布:2025-07-01 23:12:38 瀏覽:691
明日之後如何獲得最新伺服器 發布:2025-07-01 23:12:35 瀏覽:50
tv加密頻道 發布:2025-07-01 23:10:58 瀏覽:623
如何找到5d4通信密碼 發布:2025-07-01 23:03:35 瀏覽:233
華為pop伺服器地址怎麼填 發布:2025-07-01 23:02:44 瀏覽:462
訪問量賺錢 發布:2025-07-01 22:47:58 瀏覽:383
衛士開門軟體原始密碼是多少 發布:2025-07-01 22:42:17 瀏覽:958