String Methods in C#

Doganaker
baakademi
Published in
5 min readAug 30, 2020

--

In this post we will be dealing with string methods, however before jumping into the subject let’s review the data type “strings” we will use .

Strings are data types that can store text or collection of characters between double quotes. You can set a variable to string type to hold a text.

string text = "Hello World!";

Besides storing texts, strings have some methods and properties for you to manipulate and operate on them. Let’s go through these methods.

  1. Length Property:

Actually this one is more of a property of a string rather than a method. However it is commonly used and worth a mention. So, this property returns an integer(int) value and gives you the number of characters which input string contains(Number of characters gives you the length of a string). For example :

string text = "Hello"; 
Console.WriteLine(text.Length);

This will print to the console “5” as an integer(int) because the string “Hello” contains five characters.

2. ToUpper Method:

This method returns the input string but in uppercase characters. Let’s see how in the following example:

string text = "dogan";
string result = text.ToUpper();
Console.WriteLine(result);

Output of this code is “DOGAN”. As you can see all of the characters converted to uppercase characters.

3. ToLower Method:

ToLower method is the opposite of ToUpper method, so it returns a copy of the input string and convert them to lowercase characters.

string text = "DOGAN";
string result = text.ToLower();
Console.WriteLine(result);

Here you will see “dogan” on the console, converted to lowercase characters.

4. Trim Method:

This method helps you remove all leading and trailing whitespace characters, returns a string. Trim method can take a parameter with single quote(single quote (‘ ’) represents characters in C#), if you do not give a parameter the method removes whitespace by default.

string text = "****************Hello****************";
string result = text.Trim('*');
Console.WriteLine(result);

This will print out “Hello”, removed the character ‘*’ from both right and left sides. Another example:

string text = "             Hello            ";
string result = text.Trim();
Console.WriteLine(result);

This will also print out “Hello”, however we did not give a parameter to Trim() method so it removed whitespace.

5. TrimStart Method:

TrimStart() is the same as Trim() method only it removes the leading whitespace characters. This method also takes a parameter and returns a string.

string text = "************Hello**************";
string result = text.TrimStart('*');
Console.WriteLine(result);

Output is “Hello**************”, removed the character ‘*’ from the right side.

6. TrimEnd Method:

As you can guess this one removes the trailing whitespace characters, also can take a parameter and returns a string.

string text = "************Hello**************";
string result = text.TrimEnd('*');
Console.WriteLine(result);

Again the output is “************Hello”, however this time ‘*’ character removed from the left side.

7. Replace Method:

This method replaces character or set of characters in a string. Replace method takes two parameters. First parameter is the character you want to change and the second one is the character you want to change with. Let’s see it with an example:

string text = "replace";
string result = text.Replace('a','x');
Console.WriteLine(result);

Here this will print out “replxce”, replaced ‘a’ with ‘x’. Let’s also see it with set of characters…

string hero = "Spiderman";
string result = hero.Replace("man","woman");
Console.WriteLine(result);

The output is “Spiderwoman”, “man” replaced with “woman”. Also take a good look at the parameter given here with double quotes because as mentioned above this method can take a set of characters.

8. IndexOf Method:

IndexOf method helps you to find the index of a character in a string. This method takes a char(character) parameter and returns an int(integer) value. Reminder that in C# indices starts from 0.

string text = "Hello";
int indexof = text.IndexOf('e');
Console.WriteLine(indexof);

This will print out “1” to the console, as character ‘e’ is in the index[1]. However if the character is not in the string, the method will return -1 meaning the character does not exist in the string.

string text = "Hello";
int indexof = text.IndexOf('a');
Console.WriteLine(indexof);

Output is “-1” because character ‘a’ does not exist within the “Hello” string.

9. PadRight Method:

This method adds whitespace or given character to the right side. It takes one int(integer) parameter and one optional char(character) parameter. Int parameter tells the method how much whitespace it should add to the right side. Optional char parameter tells which character it should add as much as the given int value.

string text = "Hello";
string result = text.PadRight(5, 'a');
Console.WriteLine(result);

The output of this is “Helloaaaaa”. The method added five character ‘a’ to the right side of the string.

10. PadLeft Method:

The same method as PadRight() but this will add whitespace to the left side of the string.

string text = "Hello";
string result = text.PadRight(5,'*');
Console.WriteLine(result);

This code will print out “*****Hello”, the ‘*’ character added to the left side of the string.

11. Substring Method:

Substring method returns the same string but starting from the given index. So this method takes an int(integer) parameter to tell the method which index to start and print it out to the end of the string. The method also takes an optional int parameter to tell the method how much character to print.

string text = "Hello World!";
string result = text.Substring(5);
Console.WriteLine(result);

Here we will get “World!” because we told the method to get the substring starting from the index[5].(Spaces in strings also count as an index.) Let’s examine another example with optional parameter:

string text = "Hello World!";
string result = text.Substring(5,4);
Console.WriteLine(result);

This will print out “Worl” because we told the method to start from the index[5] and continue four more characters.

12. Remove Method:

This method removes all the characters after the given index. Remove method takes two int parameters, one is for the index and the other(optional) for the length. Returns a string.

string text = "Hello World!";
string result = text.Remove(5);
Console.WriteLine(result);

The output will be “Hello”, after index[5] all characters were removed. Another example:

string text = "Hello World!";
string result = text.Remove(5,3);
Console.WriteLine(result);

Here the output will give us “Hellorld!”, characters removed starting from index[5] and the following two characters.

13. Split Method:

Split method splits the string from a specified character into substrings and append them to an array. This method takes a char parameter and returns a string array. Let’s examine this method with an example:

string text = "İstanbul,Barcelona,New York,Amsterdam,Rio,Berlin";
string[] cities = text.Split(',');
for(int i = 0; i < cities.Length; i++)
{
string result = cities[i];
Console.WriteLine(result + " ");
}

This will give you the list of cities defined in the string. The output is

“İstanbul

Barcelona

New York

Amsterdam

Rio

Berlin”

Here you can see that the method splitted the string from ‘,’ characters. Also:

string text = "İstanbul,Barcelona,New York,Amsterdam,Rio,Berlin";
string[] cities = text.Split(',');
Console.WriteLine(cities);

This will print “System.String[]” indicating that Split() method returned a string array.

These methods are the most used for operating on strings. String methods are really helpful while developing, a lot of strings may require manipulation according to the application and these methods will come in handy at that point.

--

--