Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# replace multiple characters

You could use Linq's Aggregate function:

string s = "the
quick	brown
dog,jumped;over the lazy fox.";
char[] chars = new char[] { ' ', ';', ',', '
', '	', '
' };
string snew = chars.Aggregate(s, (c1, c2) => c1.Replace(c2, '
'));
Here's the extension method:

public static string ReplaceAll(this string seed, char[] chars, char replacementCharacter)
{
    return chars.Aggregate(seed, (str, cItem) => str.Replace(cItem, replacementCharacter));
}
Extension method usage example:

string snew = s.ReplaceAll(chars, '
');
Comment

replace multiple characters in string c#

string name="$1,300";
Console.Write((name.Replace("$","").Replace(",",""))); // output->  1300
Comment

c# string replace multiple matches with one charactar

public static class ExtensionMethods
{
   public static string Replace(this string s, char[] separators, string newVal)
   {
       string[] temp;

       temp = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);
       return String.Join( newVal, temp );
   }
}
// use 
char[] separators = new char[]{' ',';',',','
','	','
'};
string s = "this;is,
a	


test";

s = s.Replace(separators, "
");
Comment

c# string replace multiple matches with one charactar

s/[;,	
 ]|[
]{2}/
/g
Comment

PREVIOUS NEXT
Code Example
Csharp :: asp.net mvc hide div from controller 
Csharp :: asp.net core relative file path within console app 
Csharp :: global variable startup file .net core api 
Csharp :: how to extract unique years from a list of different years in c# 
Csharp :: c# fileinfo filename without extension 
Csharp :: c# default parameter 
Csharp :: revision1 
Csharp :: for loop cs 
Csharp :: unity 3d animator live link 
Csharp :: mvc validate 
Csharp :: enum in method as argument c# 
Csharp :: How to get the value of an input button in an ASP.NET Core MVC controller 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: set data annotation text in model c# 
Csharp :: close windows by esc wpf 
Csharp :: add file to combobox c# 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: check that IEnumerable is not empty 
Csharp :: delegates in c# 
Csharp :: dotcms contentidentifier 
Csharp :: visual studio private field underscore 
Csharp :: jtoken value is not exact double 
Csharp :: c# extract after what is 
Csharp :: linq dynamic order by descending 
Csharp :: ignore warning openxml 
Csharp :: C# read GroupComponent Or PartComponent using regex 
Csharp :: how to pass id to modal in asp.net mvc 
Csharp :: CRUD configuration MVC with Firebase 
Csharp :: c# nuint 
Csharp :: .net entities query multiple join condition 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =