0

I have the following c# code:

string myString = "TRANSACTION HAS BEEN COMPLETED";
bool check = myString.Contains("TRANSACTION");

On my dev machine and local servers check will be true. But now we have found that on a server in Turkey this returns false.

Not sure if this is related to regional settings, but is there a better way to check for substrings that are more safe to Culture/Language?

Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
  • 1
    Can you provide the exact string where the mismatch occurs? String.Contains [always uses a culture-insensitive "Ordinal" comparison](https://referencesource.microsoft.com/#mscorlib/system/string.cs,428c5c9954dea844), so your example string "TRANSACTION" should return true independent of the system culture. (See [related question](https://stackoverflow.com/q/16246189/87698).) – Heinzi Jan 26 '23 at 08:25
  • @JonathanWillcock I understand that the local culture in e.g. Turkey transforms some characters. But in this case `myString` contains an exact copy of `"TRANSACTION"` (the searched string). Any transformation done on `myString` will apply to the searched string as well, so I can't see how it explains the OPs issue. Am I wrong ? – wohlstad Jan 26 '23 at 08:27
  • 1
    @wohlstad No you are not wrong, but I was presuming that the OP has simplified the situation for illustrative purposes. I strongly doubt that the string being tested is a literal string in the same way. After all the code as presented is fairly idiotic! – Jonathan Willcock Jan 26 '23 at 08:31
  • @CameronCastillo please give us some more details about the actual strings you are comparing. – wohlstad Jan 26 '23 at 08:36
  • 1
    Try to use `String.Contains("TRANSACTION", StringComparison.InvariantCulture);` or Try to use `myString.Contains("TRANSACTION", StringComparison.InvariantCultureIgnoreCase);`if it works for your task. It may help. If not - it would be usseful to create a sample code with setting current thread culture to tr-tr which reproduce the issue and fill the bug here: https://github.com/dotnet/runtime – Vyacheslav Benedichuk Jan 26 '23 at 08:38
  • Exact string is "TRANSACTION ABC STARTED". And I'm searching for TRANSACTION. – Cameron Castillo Jan 26 '23 at 08:38
  • 1
    @CameronCastillo By the way, where are you getting `myString`? Is it coming from some third party software made in Turkey? Turkish alphabet has 2 `I` letters which looks very similar but have different codes. Local developers tend to use local letter which is not equal to english `I`. – Vyacheslav Benedichuk Jan 26 '23 at 08:49
  • @VyacheslavBenedichuk Valid question. It's extracted from an ERP system in Turkey. We have investigated that as best as possible and it would appear that there are no strange characters. Will do more checks. – Cameron Castillo Jan 26 '23 at 09:14
  • 1
    In Turkish there are two different 'i' letters. One with dot (bot in upper and lower case) and the other without dot (also both in upper and lower case). Basically, 'i' and 'I' are different letters. That's a difficult to detect issue. – Mike Mozhaev Jan 26 '23 at 09:19
  • Please copy-paste the strings that are not matching *directly* into your post or a comment (without re-typing or editing them) then we might be able to see if there are any differences in the characters. – Charlieface Jan 27 '23 at 01:20
  • @Charlieface, problem is that the myString get's read from a Data Table which get's passed into this specific function. Hence I will not be able to past the exact string to show if there are funny chars. What I did check is: message.Substring(0, 11) and that comes back as "TRANSACTION", so I guess not some unprintable chars hiding. – Cameron Castillo Jan 27 '23 at 09:44
  • @VyacheslavBenedichuk. I've tried this: myString.IndexOf("TRANSACTION", StringComparison.InvariantCultureIgnoreCase) >= 0 and that comes back as false as well. – Cameron Castillo Jan 27 '23 at 09:46
  • You can set a breakpoint, then copy the data out of the QuickWatch window. Clearly they are different characters, which is why I asked you to copy it *exactly* direct from the debugger – Charlieface Jan 27 '23 at 09:47

0 Answers0