作者TonyQ (骨头)
看板java
标题Re: [J2SE] html的文字能当成string用吗?
时间Sun Apr 15 15:42:49 2007
※ 引述《brianpcbest (FF4)》之铭言:
: http://udn.com/NEWS/WORLD/WOR1/3804335.shtml
: 以上是我要截取的网页
: 我在前面先宣告
: String endString = "联合报";
: 截取时
: while ((lineStr=in.readLine())!= null )
: { if(lineStr.equals(endString))
: break;
: testStr=testStr.replaceAll("<[^>]*>","");
: testStr=testStr.replaceAll(" ","");
: }
: 原本的用意是我是只想要截取新闻~避免掉其他的html码,
: 让他读到新闻结尾的联合报就跳出
: 但是这样的做法好像行不通
: 有人可以指点一下吗?谢谢
1.里面没有任何一行的字串 达成你的Replace条件後 是"联合报"
你应该要用 indexOf() 或者 matches() 做"包含有"的判断。
2.另外联合报在开头和结尾各有一个不适合放在结尾
建议你可以取这行
<p class="story"><span id="source_name" class="story">【2007/04/15 联合报】
</span><span class="author"><A href="
http://udn.com/" target="_blank">@
</A><a href="
http://udn.com/" target="_blank">
http://udn.com/</a></span> <a
href="
http://udn.com/?" target="_blank"><img
src="/1024/images/logo_small.gif" width="19" height="19" border="0"
align="absmiddle"></a></p>
replaceAll後变成 取
matches(".*【20[0-9]{2}/[0-1][0-9]/[0-3][0-9] 联合报】")
当脱离点也是种选择
可参考 String#matches
http://0rz.tw/072Ba
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.134.27.68