Thanks to visit codestin.com
Credit goes to github.com

Skip to content

String IndexOf is missing API for char with start index and StringComparison #44116

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

Open
aaronfranke opened this issue Nov 1, 2020 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Globalization backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity
Milestone

Comments

@aaronfranke
Copy link

https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof

Background and Motivation

The string IndexOf API is missing two methods for char that it has with string. It's currently not possible to perform an IndexOf operation with a char, start index, and StringComparison unless you convert the char to a string.

The following string methods have both string and char versions:

public int IndexOf(String value);
public int IndexOf(char value);
public int IndexOf(String value, int startIndex);
public int IndexOf(char value, int startIndex);
public int IndexOf(String value, int startIndex, int count);
public int IndexOf(char value, int startIndex, int count);
public int IndexOf(String value, StringComparison comparisonType);
public int IndexOf(char value, StringComparison comparisonType);

The following string methods are missing char versions as counterparts to the string versions:

public int IndexOf(String value, int startIndex, StringComparison comparisonType);
public int IndexOf(String value, int startIndex, int count, StringComparison comparisonType);

Proposed API

Add methods with these signatures to string:

public int IndexOf(char value, int startIndex, StringComparison comparisonType);
public int IndexOf(char value, int startIndex, int count, StringComparison comparisonType);

Usage Examples

int i = "Hello World".IndexOf('L', 5, StringComparison.OrdinalIgnoreCase); // i = 9

Which should behave identically to how it currently works with strings:

int i = "Hello World".IndexOf("L", 5, StringComparison.OrdinalIgnoreCase); // i = 9

Alternative Designs

None.

Risks

None.

@aaronfranke aaronfranke added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 1, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Nov 1, 2020
@ghost
Copy link

ghost commented Nov 1, 2020

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

@GrabYourPitchforks
Copy link
Member

GrabYourPitchforks commented Nov 1, 2020

FWIW, the scenario is currently achievable via the CompareInfo class.

int idx = CultureInfo.InvariantCulture.CompareInfo.IndexOf("Hello world", 'L', 5, CompareOptions.OrdinalIgnoreCase);

When CompareOptions.OrdinalIgnoreCase is provided to the above API, we ignore the fact that the method is chained off of InvariantCulture, and it's a strict ordinal-case-insensitive comparison.

I'm not really a fan of adding more StringComparison-based APIs off the string class, especially when the needle is a single char instead of a standalone substring. In these cases I think the caller is best served by a bool ignoreCase parameter rather than a full-on StringComparison parameter.

@aaronfranke
Copy link
Author

@GrabYourPitchforks That's actually the use case I have in mind. In Godot we have this extension method:

public static int Find(this string instance, string what, int from = 0, bool caseSensitive = true)
{
    return instance.IndexOf(what, from, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
}

@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Nov 1, 2020
@tarekgh tarekgh added this to the Future milestone Nov 1, 2020
Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Globalization backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity
Projects
None yet
Development

No branches or pull requests

5 participants