Home > Article > Backend Development > C# String Functions
Strings are the most essential part of C# programming language, and also is one of the important data types in modern languages including C#. The string data type is defined in the .NET base class library and it is a collection of characters in which each character is a Unicode character. The keyword string is an object of System. String type, which is used to denote a sequential collection of characters that is called a text and the string. The keywords consist of two types called string and String to declare string variables. Both string and String are comparably equal, so you can make use of whichever the naming convention you like better to define string variables. To avoid NullReferenceException, by initializing strings with the Empty value in case of null.
Predefined string functions are available in C# programming, Let’s see how to use string function in C# programming with the help of examples
Clone returns an instance of String. In other words, it returns another copy of that data. The return value will be merely another view of similar data. The Clone() method does not take any parameters.
Example:
String _string1="StringFunctions"; String _string2 = (String)_string1.Clone(); // To display both strings Console.WriteLine("String : {0}", _string1); Console.WriteLine("Clone String : {0}", _string2);
Output:
String : StringFunctions
Clone String : StringFunctions
CompareTo() method is used to compare the string instance with a particular String object. It checks whether the String occurrence appears in the same position as the particular string or not. Once comparing to strings it returns an integer value as output.
Example:
string _string1 = "Welcome"; string _string2 = " Welcome "; string _string3 = "C# Coding"; Console.WriteLine(_string1.CompareTo(_string2)); Console.WriteLine(_string2.CompareTo(_string3));
Output:
0
1
Contains() method is used to return a value signifying whether the particular substring presents within this string or not. If the particular substring is found in this string, it returns true otherwise false. The return value of this method is either true or false a Boolean value.
Example:
string _string1 = " Welcome "; string _string2 = " Welcome "; string _string3 = "StringFunctions"; Console.WriteLine(_string1. Contains(_string2)); Console.WriteLine(_string2. Contains(_string3));
Output:
True
False
EndsWith() method is used to verify whether the particular string matches the end of this string or not. If the particular string is present at the end of this string, then the result will be true otherwise false. The return value of this method is either true or false a Boolean value.
Example:
string _string1 = " Welcome "; string _string2 = " ome "; string _string3 = "ing"; Console.WriteLine(_string1. EndsWith(_string2)); Console.WriteLine(_string2. EndsWith(_string3));
Output:
True
False
Equals() method is used to compare whether two particular String objects have an identical value or not. If both strings have similar value, it returns true otherwise false. The return value of the Equals() method is either true or false a Boolean value.
Example:
string _string1 = " Welcome "; string _string2 = " Welcome "; string _string3 = "Strings"; Console.WriteLine(_string1. Equals(_string2)); Console.WriteLine(_string2. Equals(_string3));
Output:
True
False
GetHashCode() method is used to getting the hash code of a specified string. It returns an integer value. The return value of GetHashCode() is the hash code of a string object.
Example:
string _ string1 = "String Functions"; Console.WriteLine(_string1.GetHashCode());
Output:
1085385658
GetType() method is used to obtain the type of current object. It returns the System. Type of current instance which is used for reflection.
Example:
string _string1 = "String Functions"; Console.WriteLine(_string1.GetType ());
Output:
System.String
IndexOf() is used to get the index of the particular character present in the string. It returns the index position of the first occurrence of a particular character as an integer value.
Example:
string _string1 = "String Functions"; int index = _string1.IndexOf('t'); Console.WriteLine(index);
Output:
1
This C# string function is used to convert a string into lowercase. It returns a string in lower case. The return value of ToLower () is a string.
Example:
string _string1 = "String Functions"; string _string2 = _string1.ToLower(); Console.WriteLine(_string2 );
Output:
string functions
ToUpper() method is used to convert the string into uppercase. The return value of ToUpper () is a string.
Example:
string _string1 = "String Functions"; string _string2 = _string1.ToUpper(); Console.WriteLine(_string2 );
Output:
STRING FUNCTIONS
Insert() method is used to insert the particular string at a specified index number. The index number starts from 0. After inserting the particular string, it returns a new modified string. The return value of Insert() is a new modified string.
Example:
string _string1 = "String Functions"; string _string2 = _string1.Insert(6,"-"); Console.WriteLine(_string2 );
Output:
String- Functions
Length is a string property that returns a number of characters in a string and here spaces count as characters.
Example:
string _string1 = "String Functions"; Console.WriteLine(_string1.Length);
Output:
16
This string function in C# is used to replaces the character to get another string in which all occurrences of a particular character in this string are replaced with another specified character.
Example:
string _string1 = "Strings in F#"; string _string2 = _string1.Replace('F','C'); Console.WriteLine(_string2 );
Output:
Strings in C#
Split() method is used to split the string based on the specified value of characters in an array. The return value of this method is the string array.
Example:
string _string1 = "Welcome C Sharp"; string[] _string2 = _string1.Split(' '); foreach (string _string3 in _string2) { Console.WriteLine(_string3); }
Output:
Welcome
C
Sharp
SubString() method is used to retrieve a substring from the current occurrence of the String. The parameter “startIndex” will denote the initial position of substring and then substring will continue to the end of the string. The return value type is System. String.
Example:
string _string1 = " Hello C Sharp"; string _string2 = _string1.Substring(5); string _string3 = " StringFunction"; string _string4 = _string3.Substring(0,8); string _string5 = " StringFunction"; string _string6 = _string5.Substring(6,4); Console.WriteLine(_string2); Console.WriteLine(_string4); Console.WriteLine(_string6);
Output:
C Sharp
StringFu
Func
In this article, we learned the basics of strings in C# and how to use the String functions available in C#. Hope this article would have helped out you in understanding String Methods using C#
The above is the detailed content of C# String Functions. For more information, please follow other related articles on the PHP Chinese website!