String.StartsWith does not work when the next character is Prime Symbol (char) 697

I am trying to use a string with the main character in it, but I'm having some problems with the String.StartsWith method. Why does the following code throw an exception?

string text_1 = @"123456"; string text_2 = @"ʹABCDEF"; string fullText = text_1 + text_2; if (!fullText.StartsWith(text_1)) { throw new Exception("Unexplained bad error."); } 

I suspect the problem is that this main character (char) 697 is treated as an accent and therefore changes the letter before it. (I don’t think it should be - it should be the main character , and therefore, do not change the numbers in front of it). I'm not quite sure how to do this. I tried the method suggested in this answer , but it returned false:

 IsLetterWithDiacritics(text_1[5]) // == False IsLetterWithDiacritics(fullText[5]) // == False IsLetterWithDiacritics(fullText[6]) // == False 

Thanks for any help.

+7
string c # startswith escaping diacritics
source share
2 answers

ʹ or MODIFIER LETTER PRIME is an interval modifier letter. This is not a true character, but a special use character that modifies the previous character.

From MSDN :

The letter-modifier is a free-standing symbol of the interval, which, as a combination symbol, indicates modifications to the previous letter.


string.StartsWith returns false, because in your concatenated string 6 is actually modified with a simple character, which is added after it.

+3
source share

From MSDN :

When you call a string comparison method, such as String.Compare, String.Equals or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. For more information, see String Recommendations in the .NET Framework.

You should use StringComparison.Ordinal if you want to do a non-linguistic comparison. The code below will not throw an exception.

 string text_1 = @"123456"; string text_2 = @"ʹABCDEF"; string fullText = text_1 + text_2; if (!fullText.StartsWith(text_1, StringComparison.Ordinal)) { throw new Exception("Unexplained bad error."); } 
+2
source share

All Articles