• Post Reply Bookmark Topic Watch Topic
  • New Topic

String normalize?  RSS feed

 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has got to be easy. How come replaceAll("\\s+", "") fails?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess - are you remembering that Strings are immutable, and therefore this method doesn't affect the original String, but rather returns a new String?

If that's not the issue, I suggest posting a slightly more complete example of exactly what you're doing here, what's the expected result, and what's the actual result.
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jim,

Long time no type.

I'm thinking that I have some characters that are confusing.

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's hard to tell for sure here. I think the most useful thing you could do is add code to print out exactly what values you had for description and bodyText, and maybe the blurb as well. In several projects I've added custom asserts like the following:

I'm pretty sure at least one common test framework has created virtually the same method, but I'm too lazy to look it up right now. The point is, if it fails, you get the useful info about exactly what the values were when it failed. Including braces [] to delimit the beginning and end, in case there's extra whitespace attached to one of the values which is not obvious. There are still some cases where it's hard to see a difference visually - perhaps one value has a tab where the other has multiple spaces; that's not always obvious. You may have to devise ways to compare the strings even more carefully to discover why the description is not contained in the body. But the point is, I think there's a very good chance that if you can see just how the expected and actual values differ, you will have a very useful clue to what the cause of the problem is.
[ July 28, 2006: Message edited by: Jim Yingst ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to be a character a null char in my string surrounded by whitespace.





I also tried replaceAll("\u0000", "").

What is this char in my string?
[ July 28, 2006: Message edited by: Garrett Smith ]
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can print out its numeric representation by casting it to int first. My guess is that it's an NBSP (160 or hex A0); Java doesn't consider that a whitespace character.
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan, that was the problem; nbsp was \u00a0 and it is not considered to be whitespace.

descrption.replaceAll("\u00a0", "");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!