You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe the problem.
Currently, when we need to perform replacements in strings, we rely on .Replace(), which requires either a specific string value (string.Replace()) or a regular expression (new Regex.Replace()). While this works, it feels like there would be a benefit to having additional overrides for string.Remove() to handle these scenarios, rather than just the current option that requires specifying a starting and ending index. While the existing approach has its use cases, removing parts of strings—especially with patterns or substrings—feels like a more common need and would provide more flexibility.
Describe the solution you'd like
Below is an extension method class to server as a working example, but of course it's up to the team's interpretation as to how would be the best to implement it.
usingSystem.Text.RegularExpressions;namespaceTestConsoleApp.Extensions{publicstaticclassCommonExtensions{// Remove string or regex pattern from the string, with an optional comparison rule and regex flagpublicstaticstringRemove(thisstringsourceString,stringstringToRemove,boolisRegex=false,StringComparisoncomparisonRule=StringComparison.Ordinal){if(isRegex){returnsourceString.Remove(newRegex(stringToRemove));}returnsourceString.Replace(stringToRemove,"",comparisonRule);}// Remove a collection of strings or regex patternspublicstaticstringRemove(thisstringsourceString,stringstringToRemove,chardelimitedCharacter,boolisRegex=false,StringComparisoncomparisonRule=StringComparison.Ordinal){foreach(varpartToRemoveinstringToRemove.Split(delimitedCharacter)){sourceString=sourceString.Remove(partToRemove,isRegex,comparisonRule);}returnsourceString;}publicstaticstringRemove(thisstringsourceString,IEnumerable<string>stringsToRemove,boolisRegex=false,StringComparisoncomparisonRule=StringComparison.Ordinal){foreach(varpartToRemoveinstringsToRemove){sourceString=sourceString.Remove(partToRemove,isRegex,comparisonRule);}returnsourceString;}// Remove a single regex patternpublicstaticstringRemove(thisstringsourceString,RegexremovalRegex)=>removalRegex.Replace(sourceString,"");// Remove a single regex pattern with custom optionspublicstaticstringRemove(thisstringsourceString,RegexremovalRegex,Func<RegexOptions>options)=>newRegex(removalRegex.ToString(),options()).Replace(sourceString,"");// Remove multiple regex patternspublicstaticstringRemove(thisstringsourceString,IEnumerable<Regex>regexPatternsToRemove){foreach(RegexregexPatterninregexPatternsToRemove){sourceString=sourceString.Remove(regexPattern);}returnsourceString;}// Remove multiple regex patterns with custom optionspublicstaticstringRemove(thisstringsourceString,IEnumerable<Regex>regexPatternsToRemove,Func<RegexOptions>options){foreach(RegexregexPatterninregexPatternsToRemove){sourceString=sourceString.Remove(regexPattern,options);}returnsourceString;}}}
Additional context
There are many situations where we need to sanitize a string for various reasons. Instead of writing and maintaining minimal but still duplicated code for sanitization, it would be far more efficient to simply "Remove" whatever values we don't want. The ability to provide either a single Regex or string, or a collection of Regex or string values to remove from the source string, would simplify this process. This feature would be particularly useful for quick and easy sanitization of usernames, passphrases, or any other user input.
For example, Ally Financial provides an API endpoint (https://secure.ally.com/assets/json/invalid-strings.json) that returns a list of words or phrases they do not allow in usernames or passphrases. Note: This list includes terms that are explicitly filtered for user-provided content and may contain offensive or vulgar language. This API is called after a user successfully signs in and is taken to their dashboard. The returned list is then used to validate or sanitize user-provided input (for very good reason). Instead of manually implementing and maintaining duplicate logic to filter out these words, with the inclusion of these additional overrides, they could simply pass in their list of restricted terms and proceed with their process. This approach also makes it easier to remove characters that could potentially expose the system to injection vulnerabilities.
For instance, if Ally didn’t want users to manually clean up their passphrases, they could sanitize a phrase like:
The result would be the sanitized password: ThisIsMyPassword!, which they could store securely. Then, during login, they could call the same function to sanitize the entered password (which may contain vulgar or restricted terms), and compare it to the sanitized stored password. This would allow the user to log in with their original password, while the system ensures it matches the sanitized version stored securely, keeping the process transparent to the customer. This approach simplifies string sanitization without duplicating logic. Note: I recognize that this example wouldn’t be a best practice, and I wouldn't recommend or use it myself. It simply serves to demonstrate one way they could utilize these new overrides.
Overall, this would be a valuable quality-of-life improvement and would better align the semantic meanings of "Replace" vs "Remove". Currently, "Replace" is used in cases where the intention is not to replace, but rather to remove content. Clarifying this distinction would make the API more intuitive and consistent with its intended behavior.
The text was updated successfully, but these errors were encountered:
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Currently, when we need to perform replacements in strings, we rely on
.Replace()
, which requires either a specific string value (string.Replace()
) or a regular expression (new Regex.Replace()
). While this works, it feels like there would be a benefit to having additional overrides forstring.Remove()
to handle these scenarios, rather than just the current option that requires specifying a starting and ending index. While the existing approach has its use cases, removing parts of strings—especially with patterns or substrings—feels like a more common need and would provide more flexibility.Describe the solution you'd like
Below is an extension method class to server as a working example, but of course it's up to the team's interpretation as to how would be the best to implement it.
Additional context
There are many situations where we need to sanitize a string for various reasons. Instead of writing and maintaining minimal but still duplicated code for sanitization, it would be far more efficient to simply "Remove" whatever values we don't want. The ability to provide either a single
Regex
orstring
, or a collection ofRegex
orstring
values to remove from the source string, would simplify this process. This feature would be particularly useful for quick and easy sanitization of usernames, passphrases, or any other user input.For example, Ally Financial provides an API endpoint (https://secure.ally.com/assets/json/invalid-strings.json) that returns a list of words or phrases they do not allow in usernames or passphrases. Note: This list includes terms that are explicitly filtered for user-provided content and may contain offensive or vulgar language. This API is called after a user successfully signs in and is taken to their dashboard. The returned list is then used to validate or sanitize user-provided input (for very good reason). Instead of manually implementing and maintaining duplicate logic to filter out these words, with the inclusion of these additional overrides, they could simply pass in their list of restricted terms and proceed with their process. This approach also makes it easier to remove characters that could potentially expose the system to injection vulnerabilities.
For instance, if Ally didn’t want users to manually clean up their passphrases, they could sanitize a phrase like:
"ThisBadWordIsOtherBadWordMyLastBadWordPassword!"
They could then call:
Or, for case-insensitive matching:
The result would be the sanitized password: ThisIsMyPassword!, which they could store securely. Then, during login, they could call the same function to sanitize the entered password (which may contain vulgar or restricted terms), and compare it to the sanitized stored password. This would allow the user to log in with their original password, while the system ensures it matches the sanitized version stored securely, keeping the process transparent to the customer. This approach simplifies string sanitization without duplicating logic. Note: I recognize that this example wouldn’t be a best practice, and I wouldn't recommend or use it myself. It simply serves to demonstrate one way they could utilize these new overrides.
Overall, this would be a valuable quality-of-life improvement and would better align the semantic meanings of "Replace" vs "Remove". Currently, "Replace" is used in cases where the intention is not to replace, but rather to remove content. Clarifying this distinction would make the API more intuitive and consistent with its intended behavior.
The text was updated successfully, but these errors were encountered: