2

How to remove last white space from the string. I am reading the html code. here I want to read only 'Remove a Speed Dial: Tap the'. This is my expected output. In my code I used the trim() also. but by using below code, I am getting output as 'Remove a Speed Dial: Tap the '

   in html text is "Remove a Speed Dial: Tap the "
   String str = new String(data);
   System.out.println(Arrays.toString(data));
   System.out.println(str.trim());

  output:
  [R, e, m, o, v, e,  , a,  , S, p, e, e, d,  , D, i, a, l, :,  , T, a, p,  , t, h, e,  ]
  Remove a Speed Dial: Tap the 
archita
  • 21
  • 4
  • please don't replace all &nbsp. because I am reading html file. so need to ignore only last one. – archita Sep 07 '21 at 06:34
  • 1
    Does this answer your question? [How to remove " " from java string](https://stackoverflow.com/questions/3318404/how-to-remove-nbsp-from-java-string) – Ken Y-N Sep 07 '21 at 06:35
  • You need to show a complete, runnable program which reproduces your problem. And why not just use trim()? – tgdavies Sep 07 '21 at 06:37
  • See [How to trim no-break space in Java?](https://stackoverflow.com/questions/28295504/how-to-trim-no-break-space-in-java) –  Sep 07 '21 at 06:37
  • How do you know that ` ` is the _last_ one in your html file? You could try a regex that uses a negative look-ahead to match only the last occurence (not followed by any other occurence). – Thomas Sep 07 '21 at 06:37
  • Please also add your first comment to the question itself via an [edit]. It might get lost/overlooked otherwise. – Thomas Sep 07 '21 at 06:40
  • @user16320675 i am not sure if it is guaranteed to be the very last character in the HTML data – Sean Powell Sep 07 '21 at 07:20

1 Answers1

0

If you only want to match the last &nbsp then the following regex should be able to match it \p{Z}(?!.*\p{Z}) which if combined with

string.replaceAll("\\p{Z}(?!.*\\p{Z})","")

or if encoded

string.replaceAll(" (?!.* )","")

will remove only the last white space or invisible separator character if using \P{Z} which &nbsp is apart of, or will removed only the last   if only looking for the encoded version.

Be careful if using the \P{Z} as if there is a trailing whitespace that will be returned instead of the &nbsp


  • \p{z} - matches any kind of white space or invisible separator which includes &nbsp
  • (?!.*\p{Z}) - uses a negative look ahead which matches all of the white spaces or invisible separators before the last occurrence and consumes them so only the final occurrence is returned by the regex

EDIT: i checked &nbsp is included

Sean Powell
  • 564
  • 4
  • 20
  • Thanks. I checked with string.replaceAll("\\p{Z}(?!.*\\p{Z})","") this is working fine and displayed expected result as 'Remove a Speed Dial: Tap the'. but as I said that I am reading the html file. so when html text like 'Remove icon' and in array getting [R, e, m, o, v, e, , i, c, o, n]. if I am using this replaceall() code then output is 'Removeicon'. I want output as 'Remove icon' and 'Remove a Speed Dial: Tap the' so what can i do? – archita Sep 07 '21 at 11:27
  • @archita in this case `[R, e, m, o, v, e, , i, c, o, n]` the last white space is the space between remove and icon, in that case try the encoded version that will only remove that. – Sean Powell Sep 07 '21 at 11:31
  • String str = new String(data); System.out.println(Arrays.toString(data)); – archita Sep 07 '21 at 13:49
  • @archita i don't understand? – Sean Powell Sep 07 '21 at 13:53
  • String str = new String(data); System.out.println(Arrays.toString(data)); System.out.println(str.replaceAll(" (?!.* )","")); in output i'm getting 'Remove a Speed Dial: Tap the ' and 'Remove icon' .my expected output is 'Remove a Speed Dial: Tap the' and 'Remove icon'. – archita Sep 07 '21 at 13:56
  • output: [R, e, m, o, v, e, , a, , S, p, e, e, d, , D, i, a, l, :, , T, a, p, , t, h, e,  ] Remove a Speed Dial: Tap the  [R, e, m, o, v, e, , i, c, o, n] Remove icon – archita Sep 07 '21 at 13:57
  • I can't use diff code for both. this values are fetching in loop and that value is dynamic. so I need code by using this, i can solve my both test case means expected output should be 'Remove a Speed Dial: Tap the' and 'Remove icon' – archita Sep 07 '21 at 14:00
  • I am parsing html text content using java – archita Sep 07 '21 at 14:01
  • @archita did you try just using this? `string.replaceAll(" (?!.* )","")` for both cases i mean? – Sean Powell Sep 07 '21 at 14:02
  • I tried both code which you give me first. while using string.replaceAll("\\p{Z}(?!.*\\p{Z})","") output getting: 'Remove a Speed Dial: Tap the' and 'Removeicon'. when using string.replaceAll(" (?!.* )","") output getting: 'Remove a Speed Dial: Tap the ' and 'Remove icon' – archita Sep 07 '21 at 14:13
  • My expected output is 'Remove a Speed Dial: Tap the' and 'Remove icon' – archita Sep 07 '21 at 14:13
  • this values are comes while parsing the html text content in java. so I want code, from which i am able to get my expected output. I can not use different code for both value. I need only one which pass both test case. I tried with trim() also. but that was also not worked – archita Sep 07 '21 at 14:15