我想要输出没有空格的java程序, 我的代码是 -
Source source1=new Source(new URL(sourceUrlString1));
List<Element> temp1 = source1.getElementById("main").getAllElementsByClass("grid_3 alpha");
String line = temp1.toString();
String temps = line.trim();//using this
String temps = line.replaceAll("\\s+","")// or this
if(!temps.isEmpty())
{
sendResponse(resp, "Information for:"+searchWord+":" +temps);
}
当我使用修剪时,它会在信息和。之间提供大量空间的输出 SATISH
Information for::[
SATISH
JHOTWARA (JAIPUR)
Party:CPI
S/o|D/o|W/o: Om Prakash
Age: 42
当我使用String temps = line.replaceAll("\\s+","")
;
它输出为:
Information for::[<divclass="grid_3alpha"style='background:khaki;'><h2class="main-title">SatishKumar<fontcolor=greensize=1>(Winner)
ELECTEDFROMBIHARLEGISLATIVEASSEMBLY
<divclass="grid_2alpha">Party:JD(U)<divclass="grid_2alpha">S/o|D/o|W/o:JagdishPrasad<divclass
答案 0 :(得分:0)
String temps = line.replaceAll("\\s+"," ");
每次运行会留出一个空格。
答案 1 :(得分:0)
修剪只会削减前导和尾随空格,另见here。 我按照你的意愿做了同样的事情,我用的只是
String temps = line.replaceAll(" ","");