2

I need to remove all whitespaces after <br /> in a String. I'm getting some html text from a xml document that i am wiriting out in a android app with this code:

information_view.setText(Html.fromHtml(information));.

The problem is that this html code:

&nbsp;

 Betsson vill att du ska vinna jackpotten p&aring; &ouml;ver 90 miljoner !<br />
 D&auml;rf&ouml;r &nbsp;f&aring;r du&nbsp;5 free spins&nbsp;p&aring;&nbsp;Mega fortune&nbsp;varje dag fram tills jackpotten har g&aring;tt till en lycklig vinnare.&nbsp;<br />
 <br />
 Regler och Villkor:<br />
 Bonuspengar m&aring;ste oms&auml;ttas 35 g&aring;nger innan uttag.<br />
 &nbsp;

Looks like this when printed out in the app:

    Betsson vill att du ska vinna
jackpotten på över 90 miljoner !
    Därför får du 5 free spins på Mega
Fortune varje dag fram tills jackpotten
har gått ut till en lycklig vinnare.

    Regler och villkor:
    Bonuspengarna måste omsättas 35
gånger innan uttag.

As you can see both &nbsp; and " " whitespaces occur in the html code. But replacing one of them will suffice, as i can replace &nbsp; with " " or the other way around.

I've tried to search for a few days now, all help is very appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jiimmeh
  • 73
  • 8

1 Answers1

4
String html = ...
// replace your &nbsp;
html = html.replaceAll("<br />\\s+", "<br />");

But I would prefer to parse HTML with a HTML Parser like JSoup.

As @Christoph pointed out, you could also use this:

html = html.replaceAll("<br />\\p{Space}+", "<br />");
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • I prefer a regex literal over a string to avoid that nasty double escaping but nonetheless correct solution. However, you are missing the global modifier. – Christoph Dec 10 '12 at 15:46
  • That did the trick, thanks for the fast answer. Will mark as accepted when the site allows me. – Jiimmeh Dec 10 '12 at 15:46
  • @Christoph Don't you have to escape the regex literals with double quotes, also? What do you mean by global modifier? – jlordo Dec 10 '12 at 15:49
  • @jlordo hehe, totally owned, i was reading javascript in the tag instead of java, and now was wondering why replaceAll could possibly work when the function is actually called replace in javascript^^ – Christoph Dec 10 '12 at 16:01