html截取摘要并补齐标签(htmlparser)一

 
从html里面截取摘要关键在于在截取的时候不能截断它里面的标签,所以就要想法让它在截取的时候能截全标签:方法是写一个算法让它在截取所指定长度时只计算标签

外面文本的数量而标签里面的长度不计算在内,这样才能以指定的长度截取到不会断节的标签:

  下面就是这个小算法(见笑了):

Java代码 复制代码
  1. public static String readWithTag(File filename,int length) throws IOException {   
  2.         String content = readFileByLines(filename);   
  3.         int pos = 0,len = 0,count = 0;   
  4.         String s = "";   
  5.         StringBuffer sb = new StringBuffer();   
  6.         while(true)   
  7.         {   
  8.                 if(count >= length)   
  9.                     break;   
  10.              s = content.substring(pos, pos+1);   
  11.              if(s.equals("<"))   
  12.              {   
  13.                      len = content.indexOf(">", pos)-pos;   
  14.                      for(int i=0;i<len;i++)   
  15.                      {   
  16.                              s = content.substring(pos+i, pos+i+1);   
  17.                              sb.append(s);   
  18.                      }   
  19.                      pos += len;   
  20.              }   
  21.              else  
  22.              {   
  23.                      if(count < length)   
  24.                      {   
  25.                              if(s.equals(">"))   
  26.                              {   
  27.                                  sb.append(s);   
  28.                                  pos++;   
  29.                              }   
  30.                              sb.append(s);   
  31.                              count++;   
  32.                              pos++;   
  33.                      }   
  34.              }   
  35.         }   
  36.            
  37.         return sb.toString();   
  38. }  
详情请见下一篇:html截取摘要并补齐标签二
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值