Education: (HDSE) Higher Diploma in Software Engineer Experience: 10+ yrs (Aprox) with 2 yrs as Project Manager
Monday, 25 February 2008
Replace mupltiple Characters from the string
How to Replace Special Characters like *,?, /, \, <, >, :, |," from the string to a specific character?
Use following in your C# code
//import RegularExpressions
using System.Text.RegularExpressions;
//Now use
Regex.Replace("Orginal String ","Find Characters from the string","Replace with the string");
for e.g.
//I want to replace four character '*'(star) and ','(comma) and ' '(space) and ':'(colon)
Regex.Replace("Str: I, also wants *","[*, :]","-");
System.Console.WriteLine("Str: I, also wants *");
System.Console.WriteLine(Regex.Replace("Str: I, also wants *","[*, :]","-"));
//output
Str: I, also wants *
Str- I- also wants -
Replace mupltiple Characters from the string
How to Replace mupltiple Characters from the string to a specific character?
How to Replace Special Characters like *,?, /, \, <, >, :, |," from the string to a specific character?
string[] oOriginalString = OriginalString.Split(cFndChars);
OR using single line code
OriginalString = string.Join("-",OriginalString.Split(new char[] {'*','/','\\','?','<','>',':','|','"'}));
C# Code
string funMultipleReplace(string OrgStr, char[] ReplChars)
{
//Here I have replace characters into "-". You can use your choice.
return string.Join("-",OrgStr.Split(ReplChars));
}
//---------
char[] cFndChars = {'*','/','\\','?','<','>',':','|','"'};
string oStr = "Original String: with * and ? params having < & >";
string ReplacedString = funMultipleReplace(oStr,cFndChars);
//Output before replace chars
Original String: with * and ? params having < & >
//Output after replace chars
Original String- with - and - params having - & -