Some String Functions Of C#

In this article, I am going to share some string functions of C# with the help of example. In the c# there are a lot of predefine string functions are available. Lets see some string function with example.

  1. ToUpper() - ToUpper function converts string to upper case.
    1. string myName = "myName is Shreesh";  
    2.        myName = myName.ToUpper();  
    3.        Console.WriteLine(myName);  

C#

  1. ToLower() - ToLower converts string to lower case.
    1.  string myName = "myName is SHREESH";  
    2. myName = myName.ToLower();  
    3. Console.WriteLine(myName);  
    C#
  1. Trim()- Trim function removes extra spaces from the beginning and the ending of string.
    1. string myName = "     myName is SHREESH       ";  
    2.        myName = myName.Trim();  
    3. Console.WriteLine(myName);  
    C#
  1. Contains()- Contains method return bool value, it checks whether specified string or character exist in string or not.
    1. string myName = "myName is SHREESH";  
    2.        bool  isContains = myName.Contains("SHREESH");  
    3.        Console.WriteLine(isContains);  
    C#
  1. ToCharArray()- Convert a string to array of character.
    1. string myName = "myName is SHREESH";  
    2. char[] charArray = myName.ToCharArray();  
    3. foreach(char c in charArray)  
    4.       {  
    5.     Console.WriteLine(c);  
    6. }  
    C#
  1. Substring()- substring method returns substring of a string.
    1. string myName = "myName is SHREESH";  
    2.          myName =  myName.Substring(0, 6);  
    3.     Console.WriteLine(myName);  
    C#
  1. StartsWith()- Startswith method checks whether the first character of a string is same as specified character .It returns bool value.
    1. string myName = "myName is SHREESH";  
    2.            bool isContains = myName.StartsWith("my");  
    3.            Console.WriteLine(isContains);  
    C#

  2. Split()- Split function splits the string on the supplied value.It return the array of string.
    1. string myName = "myName-is-SHREESH";  
    2.   string[] breakMysentence = myName.Split('-');  
    3.   foreach (string data in breakMysentence)  
    4.        Console.WriteLine(data);  
    C#
Ebook Download
View all
Learn
View all