Monday 25 February, 2008

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?

char[] cFndChars = {'*','/','\\','?','<','>',':','|','"'};
string[] oOriginalString = OriginalString.Split(cFndChars);
//If I want to replace with "-"
OriginalString = string.Join("-",oOriginalString);

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 - & -

No comments:

Hits4Pay