Coding Villa

Convert string to string CamelCase-UnCamelCase using regular expression in c#

Author : Hassan Shafiq

Share with Friends
Views : 964
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this sample article you will learn that how we can Convert string to string CamelCase - UnCamelCase using regular expression in c#.


Advertisements


 

 

CamelCase

 

CamelCase is a way of writing a statement in which elements are joined without spaces with each element's initial letter capitalized within the statement, and the first letter is either upper or lower case.

 

To convert given string we use regular expression methods.

 

Regex.Replace Method (String, String, String, RegexOptions)

 

This method takes four parameters first is the input string which is needed to be modified. Second is the string pattern third is the replacement string and fourth one is the regex options which specify the matching operation. This method replaces all the string elements that match a specified regular expression with the desired replacement string.

 

Return Value

 

It returns the output modified string.

 

The following sample code will describe how we can Camel and Uncamel string in C#.

 

Code

 

public static void Main()

    {

Console.WriteLine("Camel Cased");

Console.WriteLine(program.ToStringCamelCase("ILoveMyCountry"));

Console.WriteLine("Uncamel Cased");

Console.WriteLine(program.ToStringUnCamelCase("ILoveMyCountry"));

Console.ReadLine();

 

    }

static string ToStringCamelCase(object str)

    {

stringMyString = string.Empty;

if (str == null)

        {

return "";

        }

else

        {

MyString = str.ToString();

MyString.Trim();

        }

string Temp1 = Regex.Replace(" " + MyString, "([A-Z][a-z]) ", "$1", RegexOptions.None);

return Temp1;

 

    }

public static string ToStringUnCamelCase(object str)

    {

 

stringMyString = string.Empty;

if (str == null)

        {

return "";

        }

else

        {

MyString = str.ToString();

MyString.Trim();

        }

 

string Temp = Regex.Replace(MyString, "([A-Z][a-z])", " $1", RegexOptions.Compiled).Trim();

return Temp;

    }

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook