The string type represents a character array, and its length is determined by the Length property. All the character positions inside that string are enumerated from 0 to Length-1. C# provides us with many different methods to work with strings and we are going to examine ones that are used most of the time.

For the complete navigation of this series check out: C# Back to Basics.

If you want to download the source code for our examples, you can do that from here Work With Strings Source Code.

So, let’s start.

We will learn about the following topics:

Substring, IndexOf, LastIndexOf

Substring(int startIndex) is the method which returns part of the string from startIndex to the end of the string.

Substring(int startIndex, int length) is the method which returns part of the string with defined length from the startIndex.

Let’s see this in practice:

IndexOf() is the method that returns the integer value of the position of the first appearance of the character or a string in the selected string. If that value doesn’t exist, the method will return -1.

There are different overloads of this method: IndexOf(char value), IndexOf(string value), IndexOf(char value, int startIndex), IndexOf(string value, int startIndex) etc. If we use this method with the startIndex parameter, we will not search from the beginning of the string but from that position to the end:

LastIndexOf() is the method that returns the position of the last appearance of character or string value. This method has the same overloads as the IndexOf method:

Contains, StartsWith, EndsWith

Contains(string value) is the method that returns true if a string contains the value, otherwise, it will return false:

StartsWith(string value) is the method which returns true if a string starts with the value, otherwise, returns false. As opposed to this method the EndsWith(string value) method returns true if a string ends with the value, otherwise, returns false:

Remove, Insert

Remove(int startIndex) method removes characters from the string from the startIndex position to the end of the string and returns that new string. There is an overloaded method Remove(int startIndex, int count) which removes a specified number of characters from the string from the starting index position. With the count parameter we decide how many characters we want to delete:

Insert(int startIndex, string value) is the method that inserts the value into the string from the startIndex position and returns a modified string:

ToLower, ToUpper

ToLower() returns a new string with all the lower case letters:

ToUpper() returns a new string with all the upper case letters:

Examples

Example 1: Create an application which accepts the name and last name, space separated, as input, and then prints out the name in one row and last name in another row:

The result:

string example 1 String Methods in C#

Example 2: Create an application that accepts as input a sentence and removes the first and last word of that sentence:

Example 2 String Methods in C#

Conclusion

That is awesome.

We have mastered the usage of the most used string methods in C#. With the combination of these methods, we can create powerful solutions for our applications.

Stay with us in a next post, where we are talking about Conditions in C#Conditions in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.