Validate a String to be in Guid Format in .net C#

Initially, I was using a wrong mechanism to validate a string to be in GUID format. I was doing the following:

string wrongString = String.Empty;
bool IsCorrectGuid = false;
    Guid guid = new Guid(wrongString);
    IsCorrectGuid = true;

However, try-catch is an expensive process so, if you have huge no. strings to validate and most of them will not be a Guid then, this is a very bad way of validation.

The correct way is to use Regex, and if you are working on .Net 4.0 or higher then, you can also use the Guid.TryParse method to achieve the same goal. Following I have demonstrated to validate the string in both ways.


string correctString = Guid.NewGuid().ToString();
Guid guid;
bool guidResTrue = Guid.TryParse(correctString, out guid);


string correctString = Guid.NewGuid().ToString();

//returns true for correct format, case in-sensitive
Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);
bool guidResTrue = isGuid.IsMatch(correctString.ToUpper());

Of course, Guid.TryParse should be the preferred way of doing it if, you’re using .Net 4.0 or higher.

Split a String to Array of Strings and Include the Delimiters using .NET C#

To also get the delimiter of the string after the split we’ll have to use Regex.Split. Look at the following code:
string input = "SomeText,SomeText,SomeText,SomeText,SomeText,SomeText"; 
string pattern = "(,)";
string[] substrings = Regex.Split(input, pattern);


NOTE: If you remove the parentheses from the pattern, using just “,”, the delimiters will not be preserved.

You can get more info about Regex.Split here

Determine the type of an Object in JavaScript

In javascript, all the variables are declared as var. However, sometimes we need to know the type of var to implement your logic. This is how you can determine some of the data types.

//chk if an object is an array or not.
function isArray(obj) {
//returns true is it is an array
    if (obj.constructor.toString().indexOf(“Array”) > -1){
        return true;
        return false;

To get the datatype, do the following:

var boolVal = true;
var numVal = 123;
var strVal = "String";
alert(typeof boolVal)    // displays "boolean"
alert(typeof numVal)     // displays "number"
alert(typeof strVal)     // displays "string"

Set a String as Enum

There are many instances where, in an Enum we need a string literal. However you can only enter a single word (with no space) against an integer (+ve/-ve). That’s where we use the description attribute.
Look at the Enum declaration below:-

public enum States
	[System.ComponentModel.Description("New Delhi")]
	NewDelhi = 1,
	[Description("Uttar Pradesh")]
	UttarPradesh = 2
	[Description("West Bengal")]
	WestBengal = -1,

Normally this is how we bind the Enum to a DropDownList

DropDownList1.DataSource = Enum.GetNames(typeof(States));

This is what we’ll get the output for our Enum States

To get the description value of the Enum, we’ll use the following method

public static string GetEnumDescription(Enum value) 
	System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString()); 
	if (fi != null) 
		System.ComponentModel.DescriptionAttribute[] attributes = 	(System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); 
		if (attributes != null && attributes.Length > 0) 
			return attributes[0].Description; else return value.ToString();
	return value.ToString(); 

This is how we’ll do to bind the Enum description to a DropDownList

foreach (States value in Enum.GetValues(typeof(States)))
	DropDownList1.Items.Add(new ListItem(GetEnumDescription(value), value.ToString()));

Final Output:



Happy Coding …