String Functions In 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.

String.ToUpper() - ToUpper function converts string to upper case.

string myName = "myName is Shreesh";  
myName = myName.ToUpper();  
Console.WriteLine(myName);

 String.ToUpper()

String.ToLower() - ToLower converts string to lower case.

string myName = "myName is SHREESH";  
myName = myName.ToLower();  
Console.WriteLine(myName);  

String.ToLower()

String.Trim() - Trim function removes extra spaces from the beginning and the ending of string.

string myName = "     myName is SHREESH       ";  
myName = myName.Trim();  
Console.WriteLine(myName);  

String.Trim()

String.Contains() - Contains method return bool value, it checks whether specified string or character exist in string or not.

string myName = "myName is SHREESH";  
bool  isContains = myName.Contains("SHREESH");  
Console.WriteLine(isContains);  

String.Contains()

String.ToCharArray() - Convert a string to array of character.

string myName = "myName is SHREESH";  
char[] charArray = myName.ToCharArray();  
foreach(char c in charArray)  
{  
    Console.WriteLine(c);  
}

String.ToCharArray()

String.Substring() - substring method returns substring of a string.

string myName = "myName is SHREESH";  
myName =  myName.Substring(0, 6);  
Console.WriteLine(myName);  

String.Substring()

String.StartsWith() - Startswith method checks whether the first character of a string is same as specified character .It returns bool value.

string myName = "myName is SHREESH";  
bool isContains = myName.StartsWith("my");  
Console.WriteLine(isContains);  

String.StartsWith()

String.Split() - Split function splits the string on the supplied value.It return the array of string.

string myName = "myName-is-SHREESH";  
string[] breakMysentence = myName.Split('-');  
foreach (string data in breakMysentence)  
    Console.WriteLine(data); 

String.Split()

In this blog, we saw some code example of string methods. Here is a detailed tutorial on C# strings: https://www.c-sharpcorner.com/UploadFile/ff0d0f/string-in-C-Sharp-net/