Thursday, June 26, 2008

Enum

Enumeration to String & String to Enumeration


private enum Employee
{
Programmer,
TeamLead,
TechnicalManager,
ProjectManager
}

//Enumeration To String
string strDesignation = Enum.GetName(typeof(Employee), Employee.Programmer);

//String to Enumeration>
Employee enmDesignation = (Employee)Enum.Parse(typeof(Employee), "TeamLead");

Replacing Double Quotes

Using regular expression we can replace the double quotes in your string.

using System.Text.RegularExpressions;

return Regex.Replace(strWithDoubleQuotes, @"[^\w]", "");

Instead of targetting the double quotes in the string, we are targetting the word character (\w = Any word character [a-zA-Z0-9_]) in the above example.

If user needs to ignore any other special characters, then he should include in the regular expression along with the word character
If user wants to ignore "@" then,
strWithoutDoubleQuotes = Regex.Replace(strWithDoubleQuotes, @"[^\w@]", "");

Sample Output :

strWithDoubleQuotes = testingsample"s@net

result = testingsamples@net