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

Add String.Replace() method overloads to accept String Arrays #40407

Open
vsfeedback opened this issue Aug 5, 2020 · 3 comments
Open

Add String.Replace() method overloads to accept String Arrays #40407

vsfeedback opened this issue Aug 5, 2020 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Milestone

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


I’d suggest adding 2 overloads for the String.Replace() method to accept string arrays.

1. Suggestion: String.Replace(String[], String[])

This will replace all occurences from the first argument (String[]) with the second argument (String[]).
Example:

var testString = "this is just a test";
string[] search = { "this", "just", "test" };
string[] replace = { "that", "only", "plane" };
var newString = testString.Replace(search, replace); // "that is only a plane"

At the moment you could call 3x Replace() in a chain to achieve this or write a new method for that with a loop etc.

2. Suggestion: String.Replace(String[], String)

This will replace all occurences from the first argument (String[]) with the second argument (String)
Example:

var testString = "this is just a test";
string[] search = { "this", "test" };
string replace = "no";
var newString = testString.Replace(search, replace); // "no is just a no"

Original Comments

Feedback Bot on 7/1/2020, 01:33 AM:

Thank you for taking the time to provide your suggestion.  We will do some preliminary checks to make sure we can proceed further.  We'll provide an update once the issue has been triaged by the product team.

@tarekgh tarekgh transferred this issue from dotnet/standard Aug 5, 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 Aug 5, 2020
@tarekgh tarekgh added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime labels Aug 5, 2020
@Gnbrkm41
Copy link
Contributor

Gnbrkm41 commented Aug 5, 2020

Related: #29249.

Personally not a great fan of the fact that replacements happen based on the index of the item.... I personally prefer the tuple approach described in #29249.

The second one... Not sure, one use could have been for normalising line breaks, but for that the better version of it is suggested in #40315. Not sure what other use cases might exist.

@Zerotask
Copy link

Zerotask commented Aug 6, 2020

Personally not a great fan of the fact that replacements happen based on the index of the item

It would be the value, not the index.
Tuples would be an option, too. I think you have more string arrays than tuples usually, e. g. a list from a JSON file or a column from a database table. So accepting a string array would be a better solution. Although I'm not against your suggestion. As you said, that is a personal preference.

Not sure what other use cases might exist.

There are many use cases for it. I haven't thought about line breaks instead I thought about filters, e. g. a bad word filter where you have a string array with words you don't want to have in the target string and which will be replaced to something like <bad word>. A specific example would be a stream bot (e. g. for Twitch).

@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Aug 24, 2020
@joperezr joperezr added this to the Future milestone Aug 24, 2020
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.Runtime
Projects
None yet
Development

No branches or pull requests

6 participants