String Functions In C#

In this article, I am going to share C# string functions with code examples written in .NET.

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.

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


String.ToLower() - ToLower converts string to lower case.
  1.  string myName = "myName is SHREESH";  
  2. myName = myName.ToLower();  
  3. Console.WriteLine(myName);  
String.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);  
String.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);  
String.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. }  
String.Substring() - substring method returns substring of a string.
  1. string myName = "myName is SHREESH";  
  2.          myName =  myName.Substring(0, 6);  
  3.     Console.WriteLine(myName);  
String.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);  
String.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);  
In this blog, we saw some code example of string methods. Here is a detailed tutorial on C# strings: