0

i want output of my java program without whitespaces , my code is -

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);
                }

When i am using Trim it gives output with lot of space between Information and SATISH

Information for::[




SATISH


JHOTWARA (JAIPUR)

Party:CPI
S/o|D/o|W/o: Om Prakash
Age: 42

When i am using String temps = line.replaceAll("\\s+","");

it gives output as:

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 
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
satish
  • 17
  • 1
  • 5

2 Answers2

0
String temps = line.replaceAll("\\s+"," ");

will leave one space per run of spaces.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

Trim will only cut leading and trailing whitspaces see also here. I did the same like you want to, I used simply

String temps = line.replaceAll(" ","");
Community
  • 1
  • 1
Andy_Lima
  • 129
  • 12