There are Several Methods Present in the C# String Class. In This Tutorial, We Will Discuss Some of The Most Commonly Used String Methods in C#:
In C#, the string is represented as a sequence of characters. It is an object of System.String class. C# allows the users to perform different operations on a string such as a substring, trim, concatenate, etc.
The string can be declared by using the keyword string which is an alias for the System.String object.
=> Look For The Easy C# Training Guide Here
Table of Contents:
Difference Between String And string?
This question has been revolving around in the minds of many beginners. In C# the “string” keyword is a reference to System.String class. This makes both string and String equal. Hence, you are free to use any naming convention you prefer.
string a = “hello”; // defining the variable using “string” keyword String b = “World”; //defining the variable using “String” class Console.WriteLine(a+ “ “+b);
The output will be:
hello World
C# String Methods
There are several methods present in the String class. These methods help in working with different string objects. In this tutorial, we will be discussing some of the most commonly used methods.
#1) Clone( )
The clone method in C# is used to duplicate a string type object. It returns a clone of the same data as the object type.
Parameter and Return Type
The clone method does not accept any parameters but returns an object.
Clone method example
String a = "hello"; String b = (String)a.Clone(); Console.WriteLine(b);
Output
hello
Explanation
We used the Clone method to create a clone of the first string. But the clone method returns an object and an object cannot be implicitly converted into a string. Hence, we have used casting to handle this. Then we have stored it into another variable and printed it to the console.
#2) Concat( )
A concat method in C# helps combine or concatenate several strings. It returns a combined string. There are several overload methods for Concat and one can use any of these based on the logical requirement.
Some of the commonly used overload methods include:
- Concat(String, String)
- Concat(String, String, String)
- Concat(String, String, String, String)
- Concat(Object)
- Concat(Object, Object)
- Concat(Object, Object, Object)
- Concat(Object, Object, Object, Object)
Parameter and Return Type
It takes string or object as an argument and returns a string object.
Example:
string a = "Hello"; string b = "World"; Console.WriteLine(string.Concat(a,b));
Output
HelloWorld
Explanation
In this example, we have used the Concat method to combine two string variables. The concat method accepts strings as an argument and returns object. We have concatenated both the declared variables and then have printed them to the console.
#3) Contains( )
Contain method in C# is used to determine if a particular substring is present inside a given string or not. Contains method returns a Boolean value, hence if the given substring is present inside the string then it will return “true” and if it is absent then it will return “false”.
Parameters and Return Type
It accepts a string as an argument and returns Boolean value as true or false. The parameter is a substring whose occurrence needs to be validated inside the string.
Example:
string a = "HelloWorld"; string b = "World"; Console.WriteLine(a.Contains(b));
Output
True
Now, let’s see what happens if a given substring is not present inside a string.
string a = "software"; string b = "java"; Console.WriteLine(a.Contains(b));
Output
False
Explanation
In the first example, the program tried to find out if the substring “World” is present in the string “HelloWorld”. As the substring was present, it returned a Boolean value “True”.
In the second example when we tried to find if the string “java” is present inside the string “software”, then the method returned a “False” value as it couldn’t find “java” anywhere inside the “software”.
#4) Copy( )
The Copy method in C# is used to produce a new string instance with the same value as a different declared string.
Parameters and Return Type
It accepts a string as a parameter whose copy needs to be created and returns a string object.
Example:
string a = "Hello"; string b = string.Copy(a); Console.WriteLine(b);
Output
Hello
Explanation
In the above example, we declared a variable and then created a copy of it using the copy method and stored it in another variable “b”. The string.Copy() method creates a copy of a given string. We then printed the copy to the console to receive the output.
#5) Equals( )
The Equals method in C# is used to validate if the two given strings are the same or not. If both the strings contain the same value then this method will return true and if they contain different value then this method will return false. In simpler words, this method is used to compare two different strings to determine their equality.
Parameter and Return Type
It accepts a string parameter and returns a Boolean value.
Example:
When both the strings are not equal
string a = "Hello"; string b = "World"; Console.WriteLine(a.Equals(b));
Output
False
Example:
When both strings are equal
string a = "Hello"; string b = "Hello"; Console.WriteLine(a.Equals(b));
Output
True
Explanation
In the first example, we have validated two unequal strings “a” and “b”. When both the strings are not equal, the Equals method is used for validation, and it returns “False”, which we have printed to the console.
In the second example, we have tried to validate two strings with equal values. As both the values are equals, the Equals method has returned “True”, which we have printed on the console.
#6) IndexOf( )
The IndexOf method in C# is used to find the index of a specific character inside a string. This method provides an index in the form of an integer. It counts the index value starting from zero.
Parameter and Return Type
It accepts a character as a parameter and returns an integer value defining the position of the character inside the string.
Example
string a = "Hello"; int b = a.IndexOf('o'); Console.WriteLine(b);
Output
4
Explanation
In the above example, we have a string “Hello”. Using the IndexOf method we have tried to find the position of char ‘o’ in the string. The position of the index is then stored inside another variable b. We received the value of b as 4 because the char ‘0’ is present at index 4 (counting from zero).
#7) Insert( )
The Insert method in C# is used for inserting a string at a specific index point. As we learned in our earlier, the index method starts with zero. This method inserts the string inside another string and returns a new modified string as the result.
Parameter and Return Type
The insert method accepts two parameters, the first being an integer that defines the index at which the string needs to be inserted and the second one is the string that is used for insertion.
It returns a modified string value.
Example
string a = "Hello"; string b = a.Insert(2, “_World_”); Console.WriteLine(b);
Output
He_World_llo
Explanation
In the above example, we have defined a string variable with value “Hello”. Then we used the Insert method to enter another string “_World_” inside the first string at index 2. As the output shows the second string has been inserted at index 2.
#8) Replace( )
The Replace method in C# is used to replace a certain set of concurrent characters from a given string. It returns a string with characters replaced from the original string. Replace method has two overloads, it can be used to replace both strings as well as characters.
Parameter and Return Type
It accepts two parameters, the first one is the character that needs to be replaced from the given string. The second parameter is the character or string by which you want to replace the string/char in the previous parameter.
Let’s have a look at an example to clear up things.
Example:
string a = "Hello"; string b = a.Replace(“lo”, “World”); Console.WriteLine(b);
Output
HelWorld
Explanation
In the above example, we used a string variable “a” containing “Hello” as value. We then used the Replace method to remove “lo” from the first string by replacing it with the second parameter.
#9) SubString( )
The SubString method in C# is used to get a part of the string from a given string. By using this method, the program can specify a starting index and can get the substring till the end.
Parameter and Return Type
It accepts an integer parameter as an index. The index specifies the start point of the substring. The method returns a string.
Example:
string a = "Hello"; string b = a.Substring(2); Console.WriteLine(b);
Output
llo
Explanation
We passed index two in the substring method that serves as the starting point of the substring. Hence, it starts picking up the characters inside the string from index 2. Thus, we receive the output of all the characters including and after index 2.
#10) Trim( )
The Trim method in C# is used to remove all the whitespace characters at the start and end of a string. It can be used whenever a user needs to remove extra whitespace at the start or end of a given string.
Parameter and Return type
It doesn’t accept any parameter but returns a string.
Example
When both the strings are not equal
string a = "Hello "; string b = a.Trim(); Console.WriteLine(b);
Output
Hello
Explanation
We used a string where we have extra whitespace at the end. Then we used the Trim method to remove the extra whitespace and stored the value returned by Trim in another variable b. Then we printed the output to the console.
Conclusion
In this tutorial, we learned about the String class in C#. We also looked into some of the most commonly used methods from the String class. We learned how to trim, replace, close, insert, copy, etc. a string.
We also learned how to perform validations on a given string by using methods such as equals and contains.
=> Look For The Easy C# Training Guide Here