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.
- ToUpper() - ToUpper function converts string to upper case.
- string myName = "myName is Shreesh";
- myName = myName.ToUpper();
- Console.WriteLine(myName);
- ToLower() - ToLower converts string to lower case.
- string myName = "myName is SHREESH";
- myName = myName.ToLower();
- Console.WriteLine(myName);
- 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);
- 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);
- 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);
- }
- Substring()- substring method returns substring of a string.
- string myName = "myName is SHREESH";
- myName = myName.Substring(0, 6);
- Console.WriteLine(myName);
- 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);
- 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);