string.IndexOf get different result in .Net 5

前端 未结 2 1838
死守一世寂寞
死守一世寂寞 2021-02-08 07:14

When i run following code in .Net Core 3.1 i get 6

//Net Core 3.1
string s = "Hello\\r\\nwor         


        
2条回答
  •  借酒劲吻你
    2021-02-08 07:29

    Your sample code exactly matches the one posted on MSDN which also describes the why and how to revert to the old behavior in these excerpts (emphases mine):

    In the past, the .NET globalization APIs used different underlying libraries on different platforms. On Unix, the APIs used International Components for Unicode (ICU), and on Windows, they used National Language Support (NLS). [...] Behavior differences were evident in these areas:

    • Cultures and culture data
    • String casing
    • String sorting and searching
    • Sort keys
    • String normalization
    • Internationalized Domain Names (IDN) support
    • Time zone display name on Linux

    To revert back to using NLS [as relevant for Windows 10 May 2019 Update and newer which now uses ICU by default], a developer can opt out of the ICU implementation. Applications can enable NLS mode in any of the following ways:

    • In the project file:

      
        
      
      
    • In the runtimeconfig.json file:

      {
        "runtimeOptions": {
           "configProperties": {
             "System.Globalization.AppLocalIcu": ": or "
           }
        }
      }
      
    • By setting the environment variable DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU to the value : or .

      : Optional suffix of fewer than 36 characters in length, following the public ICU packaging conventions. When building a custom ICU, you can customize it to produce the lib names and exported symbol names to contain a suffix, for example, libicuucmyapp, where myapp is the suffix.

      : A valid ICU version, for example, 67.1. This version is used to load the binaries and to get the exported symbols.

    For more / up-to-date information, please refer to the MSDN link above.

    However, I recommend reading up on György Kőszeg's answer aswell, as you'd only have to worry about these details from inexact string operations to begin with.

提交回复
热议问题