【问题标题】:How to remove white space remove in list object?如何删除列表对象中的空格删除?
【发布时间】:2014-05-26 10:18:37
【问题描述】:

我想要我的 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);
                }

当我使用 Trim 时,它会在输出信息和信息之间留出大量空间 饱和度

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 

【问题讨论】:

    标签: java string dom


    【解决方案1】:
    String temps = line.replaceAll("\\s+"," ");
    

    每运行一个空格会留下一个空格。

    【讨论】:

      【解决方案2】:

      Trim 只会剪切前导空格和尾随空格,另请参阅here。 我做了你想做的一样,我只是用了

      String temps = line.replaceAll(" ","");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 2021-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多