Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Comparison, .NET and non breaking space

I have an app written in C# that does a lot of string comparison. The strings are pulled in from a variety of sources (including user input) and are then compared. However I'm running into problems when comparing space '32' to non-breaking space '160'. To the user they look the same and so they expect a match. But when the app does the compare, there is no match.

What is the best way to go about this? Am I going to have to go to all parts of the code that do a string compare and manually normalize non-breaking spaces to spaces? Does .NET offer anything to help with that? (I've tried all the compare options but none seem to help.)

It has been suggested that I normalize the strings upon receipt and then let the string compare method simply compare the normalized strings. I'm not sure it would be straight-forward to do that because what is a normalized string in the first place. What do I normalize it too? Sure, for now I can convert non-breaking spaces to breaking spaces. But what else can show up? Can there potentially be very many of these rules? Might they even be conflicting. (In one case I want to use a rule and in another I don't.)

like image 616
Mark Avatar asked Mar 31 '09 14:03

Mark


People also ask

How do you know if a space is non breaking?

The quickest way is to press Ctrl+Shift+Spacebar (or on a Mac, type Option+Spacebar). Alternatively, you can select 'Non-breaking Space' from the Insert>Special Characters menu. To work in more depth with non-breaking spaces, you need to be able to see them.

What is the difference between space and non breaking space?

Your word processor assumes that a word space marks a safe place to flow text onto a new line or page. A nonbreaking space is the same width as a word space, but it prevents the text from flowing to a new line or page. It's like invisible glue between the words on either side.

Can we compare two strings in C#?

The C# Compare() method is used to compare first string with second string lexicographically. It returns an integer value. If both strings are equal, it returns 0. If first string is greater than second string, it returns 1 else it returns -1.


2 Answers

I went through lots of pain to find this simple answer. The code below uses a regular expression to replace non breaking spaces with normal spaces.

string cellText = "String with non breaking spaces.";
cellText = Regex.Replace(cellText, @"\u00A0", " ");

Hope this helps, Dan

like image 136
user127143 Avatar answered Oct 03 '22 17:10

user127143


It needs to be

text.Replace('\u00A0',' ')

where \u00A0 is non breaking space

This will replace the non breaking space with normal space.

like image 34
Gaurravs Avatar answered Oct 03 '22 17:10

Gaurravs