Skip to content

Incorrect behavior of string.IndexOf in different cultures #111340

Closed
@rolfbjarne

Description

@rolfbjarne

From @JeroenBer on Sun, 12 Jan 2025 12:34:19 GMT

Apple platform

iOS

Framework version

net9.0-*

Affected platform version

Dotnet version 9.0.101, ios workload 18.2.9170/9.0.100, MacBook Intel Sequoia 15.2, XCode 16.2, iPhone 15 / 16 emulator

Description

After upgrading from .NET8 to .NET9 our app crashed on Exceptions. After troubleshooting the cause is:

String.IndexOf() returns incorrect values.

// Should always return i = -1
var x = "";
var i = x.IndexOf(" ")

// Since it cannot find a space in an empty string it should return -1

The above scenario should return -1 on all cultures. On iOS it returns 0 after setting cultures. This leads to problems later since the code thinks the string is found at position 0.

On .NET8 and other platform it always returns -1. Also after setting the culture. So it seems introduced with .NET 9.

Steps to Reproduce

  1. Download the repro app: https://github.com/JeroenBer/net9-culture-issues
  2. install .net9 ios workload
  3. run the app on the iOS simulator
  4. it will show the IndexOf returns 0 instead of -1.

Did you find any workaround?

There is NO good workaround. Since it cannot be guaranteed that this behavior leeds to issues in External libraries using this trivial function.
For code in own control IndexOf stringcomparison type Ordinal can solve it.

It has something to do with the culture. Please let me know if it's reproducable ? And what the cause is ?

Are there any other culture thing going wrong on .NET 9 due to the same cause ?

I think this is a very serious bug and should be solved as soon as possible in .NET 9 iOS.

Relevant log output

No response

Copied from original issue dotnet/macios#21947

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions