• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How can I trim a non-breaking space?

Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trim() method of String does not trim the non-breaking space character. I would just write my own trim method but I don't know how to iterate over the code-points of a String anymore since therre may be supplemental characters.

The following code demonstrates the problem. In my database software, if there is a non-breaking space, it is almost certainly there by mistake and I want to trim all leading and all trailing white-space, including the non-breaking space.

Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, all the characters or codepoints you might want to trim seem to be single characters, not codepoints that require surrogate pairs. So you should be able to continue treating the String as single characters, just like you would have in the past. If any surrogate pairs occur in the middle of the string, it's no problem as long as you don't change them in any way. Here's one solution:

The ^ and $ indicate the beginning and end of the string. The trim() method normally trims chars in the range 0x00-0x20, and we just added one additional character to the character class.

You could also make a faster version (probably) by taking the source code for the trim() method, and modifying it to trim 00 as well as the usual range. Of course you'd also need to modify it to work as a static method that takes a String parameter, not a method of the String class that operates on "this".
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic