Replace all occurrences of a string (in an array) with a single value

I have a string array:

string[] arr2 = { "/", "@", "&" }; 

I have another line (i.e. strValue ). Is there a clean way to replace all instances of the contents of an array with a single value (i.e., Underscore)? So before:

 strValue = "a/ new string, with some@ values&" 

And after:

 strValue = "a_ new string, with some_ values_" 

I decided to do this:

 strValue = strValue.Replace("/", "_"); strValue = strValue.Replace("@", "_"); strValue = strValue.Replace("&", "_"); 

But my array of substitution characters can become much larger.

+6
source share
6 answers

Instead of using Replace again and again, you can simply write your own. It may even improve performance, as you mentioned

But my array can get a lot more.

 public string Replace(string original, char replacement, params char[] replaceables) { StringBuilder builder = new StringBuilder(original.Length); HashSet<char> replaceable = new HashSet<char>(replaceables); foreach(Char character in original) { if (replaceable.Contains(character)) builder.Append(replacement); else builder.Append(character); } return builder.ToString(); } public string Replace(string original, char replacement, string replaceables) { return Replace(original, replacement, replaceables.ToCharArray()); } 

You can call it like this:

 Debug.WriteLine(Replace("a/ new string, with some@ values&", '_', '/', '@', '&')); Debug.WriteLine(Replace("a/ new string, with some@ values&", '_', new[] { '/', '@', '&' })); Debug.WriteLine(Replace("a/ new string, with some@ values&", '_', existingArray)); Debug.WriteLine(Replace("a/ new string, with some@ values&", '_',"/@&")); 

Output:

 a_ new string, with some_ values_ a_ new string, with some_ values_ a_ new string, with some_ values_ a_ new string, with some_ values_ 

As @Sebi noted, this will also work as an extension method:

 public static class StringExtensions { public static string Replace(this string original, char replacement, params char[] replaceables) { StringBuilder builder = new StringBuilder(original.Length); HashSet<Char> replaceable = new HashSet<char>(replaceables); foreach (Char character in original) { if (replaceable.Contains(character)) builder.Append(replacement); else builder.Append(character); } return builder.ToString(); } public static string Replace(this string original, char replacement, string replaceables) { return Replace(original, replacement, replaceables.ToCharArray()); } } 

Using:

 "a/ new string, with some@ values&".Replace('_', '/', '@', '&'); existingString.Replace('_', new[] { '/', '@', '&' }); // etc. 
+5
source

Here's how I would do it by creating a regex clause from a list of delimiters and replacing them with an underscore

 string[] delimiters = { "/", "@", "&" }; string clause = $"[{string.Join("]|[", delimiters)}]"; string strValue = "a/ new string, with some@ values&"; Regex chrsToReplace = new Regex(clause); string output = chrsToReplace.Replace(strValue, "_"); 

You probably want to encapsulate in if (delimiters.Any ()) , otherwise it will work if the array is empty

+3
source

Sure. Here is one approach:

 var newString = arr2.Aggregate(strValue, (net, curr) => net.Replace(curr, "_")); 

If you only replace individual characters and have input sizes large enough for optimization, you can create a set from which you can replace:

 var substitutions = new HashSet<char>() { '/', '@', '&' }; var strValue = "a/ new string, with some@ values&"; var newString = new string(strValue.Select(c => substitutions.Contains(c) ? '_' : c).ToArray()); 
+2
source

You can split the line with the list of lines []:

 string[] arr2 = { "/", "@", "&" }; string strValue = "a/ new string, with some@ values&"; string Output = null; string[] split = strValue.Split(arr2, StringSplitOptions.RemoveEmptyEntries); foreach (var item in split) { Output += item + "_"; } Console.WriteLine(Output); //-> a_ new string, with some_ values_ 


Updated answer with comment by @aloisdg (interesting article, thanks).
 string[] arr2 = { "/", "@", "&" }; string strValue = "a/ new string, with some@ values&"; string[] split = strValue.Split(arr2, StringSplitOptions.RemoveEmptyEntries); StringBuilder Output = new StringBuilder(); foreach (var item in split) { Output.Append(item + "_"); } Console.WriteLine(Output); //-> a_ new string, with some_ values_ 
0
source

Perhaps not the fastest, but the easiest was Select with Contains .

Something like this: source.Select(c => blacklist.Contains(c) ? letter : c)

Demo on . NetFiddle

 using System; using System.Linq; public class Program { public static void Main() { var strValue = "a/ new string, with some@ values&"; Console.WriteLine(strValue.Replace("/@&", '_')); } } public static class Extensions { public static string Replace(this string source, string blacklist, char letter) => new string(source.Select(c => blacklist.Contains(c) ? letter : c).ToArray()); } 
0
source

You can use foreach on a single line to achieve what you want:

 arr2.ToList().ForEach(x => strValue = strValue.Replace(x, "_")); 
0
source

Source: https://habr.com/ru/post/1012343/


All Articles