Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:09:08+00:00 2026-06-07T19:09:08+00:00

We have recently upgraded all our projects from .NET 3.5 to .NET 4. I

  • 0

We have recently upgraded all our projects from .NET 3.5 to .NET 4. I have come across a rather strange issue with respect to string.IndexOf().

My code obviously does something slightly different, but in the process of investigating the issue, I found that calling IndexOf() on a string with itself returned 1 instead of 0.
In other words:

string text = "\xAD\x2D";          // problem happens with "­-dely N.China", too;
int index = text.IndexOf(text);    // see update note below.

Gave me an index of 1, instead of 0. A couple of things to note about this problem:

  • The problems seems related to these hyphens (the first character is the Unicode soft hyphen, the second is a regular hyphen).

  • I have double checked, this does not happen in .NET 3.5 but does in .NET 4.

  • Changing the IndexOf() to do an ordinal compare fixes the issue, so for some reason that first character is ignored with the default IndexOf.

Does anyone know why this happens?

EDIT

Sorry guys, made a bit of a stuff up on the original post and got the hidden dash in there twice. I have updated the string, this should return index of 1 instead of 2, as long as you paste it in the correct editor.

Update:

Changed the original problem string to one where every actual character is clearly visible (using escaping). This simplifies the question a bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:09:09+00:00Added an answer on June 7, 2026 at 7:09 pm

    Your string exists of two characters: a soft hyphen (Unicode code point 173) and a hyphen (Unicode code point 45).

    Wiki: According to the Unicode standard, a soft hyphen is not displayed if the line is not broken at that point.

    When using "\xAD\x2D".IndexOf("\xAD\x2D") in .NET 4, it seems to ignore that you’re looking for the soft hyphen, returning a starting index of 1 (the index of \x2D). In .NET 3.5, this returns 0.

    More fun, if you run this code (so when only looking for the soft hyphen):

    string text = "\xAD\x2D";
    string shy = "\xAD";
    int i1 = text.IndexOf(shy);
    

    then i1 becomes 0, regardless of the .NET version used. The result of text.IndexOf(text); varies indeed, which at a glance looks like a bug to me.

    As far as I can track back through the framework, older .NET versions use an InternalCall to IndexOfString() (I can’t figure out to which API call that goes), while from .NET 4 a QCall to InternalFindNLSStringEx() is made, which in turn calls FindNLSStringEx().

    The issue (I really can’t figure out if this is intended behaviour) indeed occurs when calling FindNLSStringEx:

    LPCWSTR lpStringSource = L"\xAD\x2D";
    LPCWSTR lpStringValue = L"\xAD";
    
    int length;
    
    int i = FindNLSStringEx(
        LOCALE_NAME_SYSTEM_DEFAULT,
        FIND_FROMSTART,
        lpStringSource,
        -1,
        lpStringValue,
        -1,
        &length,
        NULL,
        NULL,
        1);
    
    Console::WriteLine(i);
    
    i = FindNLSStringEx(
        LOCALE_NAME_SYSTEM_DEFAULT,
        FIND_FROMSTART,
        lpStringSource,
        -1,
        lpStringSource,
        -1,
        &length,
        NULL,
        NULL,
        1);
    
    Console::WriteLine(i);
    
    Console::ReadLine();
    

    Prints 0 and then 1. Note that length, an out parameter indicating the length of the found string, is 0 after the first call and 1 after the second; the soft hyphen is counted as having a length of 0.

    The workaround is to use text.IndexOf(text, StringComparison.OrdinalIgnoreCase);, as you’ve noted. This makes a QCall to InternalCompareStringOrdinalIgnoreCase() which in turn calls FindStringOrdinal(), which returns 0 for both cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All - I have recently upgraded our production environment from Sql Server Reporting Services
I have recently upgraded my ASP.NET MVC application from beta to version 1. And
All: We are using Visual Studio 2010, and we have recently upgraded our workstations
I recently upgraded a C# project from .NET 3.5 to .NET 4. I have
I have recently upgraded my .Net 3.5 solution containing some C# code projects and
We have recently upgraded from Delphi 2009 to 2010. One of the things in
I have recently upgraded from websphere 5.1.2 to websphere 6.1 server. I had some
We've recently upgraded one of our SSRS2005 servers to SSRS2008 and have found that
Our dev server was recently upgraded to PHP v5.2.13. With that upgrade we have
we have a problem with our applications. We have recently upgraded our toolchain to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.