Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOf("\n") does not match "\r\n" #52936

Closed
gfs opened this issue May 18, 2021 · 2 comments
Closed

IndexOf("\n") does not match "\r\n" #52936

gfs opened this issue May 18, 2021 · 2 comments
Labels
area-System.Globalization untriaged New issue has not been triaged by the area owner

Comments

@gfs
Copy link

gfs commented May 18, 2021

The string based IndexOf("\n") and LastIndexOf("\n") do not match Windows linebreaks (\r\n) but the character based IndexOf('\n') does. This contrasts with the behavior of Contains (and the apparent documented behavior of matching any substring) which matches \r\n for both Contains('\n') and Contains("\n").

Here's the output of IndexOf and LastIndexOf compared with Contains.

"\r\n".IndexOf("\n") = -1
"\r\n".IndexOf('\n') = 1
"\r\n".LastIndexOf("\n") = -1
"\r\n".LastIndexOf('\n') = 1
"\r\n".Contains("\n") = True
"\r\n".Contains('\n') = True

Code to demonstrate the issue.

using System;

namespace IndexOfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var inputstring = "\r\n";
            var printableInput = "\\r\\n";
            Console.WriteLine($"\"{printableInput}\".IndexOf(\"\\n\") = {inputstring.IndexOf("\n")}");
            Console.WriteLine($"\"{printableInput}\".IndexOf('\\n') = {inputstring.IndexOf('\n')}");
            Console.WriteLine($"\"{printableInput}\".LastIndexOf(\"\\n\") = {inputstring.LastIndexOf("\n")}");
            Console.WriteLine($"\"{printableInput}\".LastIndexOf('\\n') = {inputstring.LastIndexOf('\n')}");
            Console.WriteLine($"\"{printableInput}\".Contains(\"\\n\") = { inputstring.Contains("\n")}");
            Console.WriteLine($"\"{printableInput}\".Contains('\\n') = { inputstring.Contains('\n')}");
        }
    }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Globalization untriaged New issue has not been triaged by the area owner labels May 18, 2021
@ghost
Copy link

ghost commented May 18, 2021

Tagging subscribers to this area: @tarekgh, @safern
See info in area-owners.md if you want to be subscribed.

Issue Details

The string based IndexOf("\n") and LastIndexOf("\n") do not match Windows linebreaks (\r\n) but the character based IndexOf('\n') does. This contrasts with the behavior of Contains (and the apparent documented behavior of matching any substring) which matches \r\n for both Contains('\n') and Contains("\n").

Here's the output of IndexOf and LastIndexOf compared with Contains.

"\r\n".IndexOf("\n") = -1
'\r\n'.IndexOf('\n') = 1
"\r\n".LastIndexOf("\n") = -1
'\r\n'.LastIndexOf('\n') = 1
"\r\n".Contains("\n") = True
'\r\n'.Contains('\n') = True

Code to demonstrate the issue.

using System;

namespace IndexOfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var inputstring = "\r\n";
            var printableInput = "\\r\\n";
            Console.WriteLine($"\"{printableInput}\".IndexOf(\"\\n\") = {inputstring.IndexOf("\n")}");
            Console.WriteLine($"'{printableInput}'.IndexOf('\\n') = {inputstring.IndexOf('\n')}");
            Console.WriteLine($"\"{printableInput}\".LastIndexOf(\"\\n\") = {inputstring.LastIndexOf("\n")}");
            Console.WriteLine($"'{printableInput}'.LastIndexOf('\\n') = {inputstring.LastIndexOf('\n')}");
            Console.WriteLine($"\"{printableInput}\".Contains(\"\\n\") = { inputstring.Contains("\n")}");
            Console.WriteLine($"'{printableInput}'.Contains('\\n') = { inputstring.Contains('\n')}");
        }
    }
}
Author: gfs
Assignees: -
Labels:

area-System.Globalization, untriaged

Milestone: -

@ghost ghost added this to Untriaged in ML, Extensions, Globalization, etc, POD. May 18, 2021
@tarekgh
Copy link
Member

tarekgh commented May 18, 2021

@gfs thanks for your report. This is a duplicate of #43736. Please have a look at the detailed discussion there and let's know of if you still have any question.

@tarekgh tarekgh closed this as completed May 18, 2021
ML, Extensions, Globalization, etc, POD. automation moved this from Untriaged to Done May 18, 2021
@dotnet dotnet locked as resolved and limited conversation to collaborators Jun 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Globalization untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants