• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I trim a non-breaking space?  RSS feed

 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • 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.



Kaydell
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!