7

I am trying to use a string with the Prime symbol in it, but I am having some issues with the String.StartsWith method. Why is the following code throwing the 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 that the issue is because this Prime symbol (char)697 is being treated as an accent and so is changing the letter before it. (I don't think it should be - it should be the the prime symbol and so should not be changing the numerical numbers in front of it). I am not exactly sure how to go about testing this. I did try the method proposed in this answer but it returned false:

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

Thanks for any help.

Community
  • 1
  • 1
Ben
  • 3,241
  • 4
  • 35
  • 49
  • 1
    Have you tried using the overload that take a `StringComparison` type and telling it to use the `InvariantCulture` or the `Ordinal`? – Bradley Uffner Oct 02 '15 at 16:34
  • 1
    @BradleyUffner That won't work, but `fullText.StartsWith(text_1, StringComparison.Ordinal)` will work. – DavidG Oct 02 '15 at 16:36

2 Answers2

3

ʹ or MODIFIER LETTER PRIME is a spacing modifier letter. It's not a true character but a special use symbol that modifies the preceding character.

From MSDN:

A modifier letter is a free-standing spacing character that, like a combining character, indicates modifications of a preceding letter.


string.StartsWith is returning false because in your concatenated string, the 6 is actually modified by the prime symbol that is appended after it.

Scorpion
  • 784
  • 7
  • 25
  • Thanks, I also just noticed this at the bottom of the Wiki page I linked. I realize now that I am using the wrong Prime Symbol - should be using (char)8242, and not the modifier letter prime. Thanks for clarifying. – Ben Oct 02 '15 at 16:44
2

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 Best Practices for Using Strings in the .NET Framework.

You should use StringComparison.Ordinal if you want to make 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.");
}
Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39