3

I'm running this small test program on .NET 6, Windows 11:

int x = "event:/SFX/Crackling/CracklingLoop".LastIndexOf("/");
if (x == 20)
    Console.WriteLine("LastIndexOf ok: " + x);
else
    Console.WriteLine("LastIndexOf failed: "+x);

int y= "event:/SFX/Crackling/CracklingLoop".IndexOf("/");
if (y == 6)
    Console.WriteLine("IndexOf ok: " + y);
else
    Console.WriteLine("IndexOf failed: "+y);

On my computer, it prints the expected values:

LastIndexOf ok: 20
IndexOf ok: 6

However, on the computers of some customers, it prints:

LastIndexOf failed: 34
IndexOf failed: 0

I'm completely baffled how this is possible. When I re-implement string.IndexOf myself, everything works as expected.

There have been changes regarding IndexOf and \n characters (previously discussed here, here and in other places). However, these strings do not contain any \n.

LTR
  • 1,226
  • 2
  • 17
  • 39
  • Is there any information about the systems your clients are running? Could potentially be an issue between 32/64-bit machines? – SimonC Jan 24 '23 at 19:14
  • https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu – SilentTremor Jan 24 '23 at 19:15
  • 4
    The results suggest `/` is treated the same as the empty string for purposes of comparing using the culture-specific collation of the user (if it truly *failed*, as in, found nothing, `.IndexOf()` would have returned -1). What does `.IndexOf("/", StringComparison.Ordinal)` give you? – Jeroen Mostert Jan 24 '23 at 19:16
  • @JeroenMostert Indeed! Using StringComparison.Ordinal makes it work correctly. The customer where it failed has their Windows set to Thai language. I will use .Ordinal everywhere. Feel free to turn your comment into an answer. – LTR Jan 24 '23 at 20:25

0 Answers0