14 Examples to Understand C# String and its Methods – trim, replace, IndexOf and others

The strings in C#

A string is a sequence of characters where each character is a Unicode character. For example, “This is a C# programming tutorial” is a simple string.

A few main points about C# strings are:

  • A string is a type of variable in C# programming.
  • Each character in the string is a Unicode character.
  • Strings are the sequence of characters.
  • C sharp Strings are objects.
  • C# has System.String class that enables you to create strings.
  • The string (starting with small ‘s’) is an alias of System.String class, that means you can create a string by using the string keyword.
  • There is no difference between string and String (capital or small ‘s’)
  • The String class has its properties and methods that can be used to perform desired tasks on strings like the length, Concat, Split, Trim, Compare etc.

This tutorial will show you how to create the strings and how to use a few useful methods of the strings.

How to create a string?

You can create the string in C# as follows:

string str = “This is c# string”;


As such, the string is an object, you can create it by using the string class constructor with the new keyword as follows:

string str = new string(array_of_Unicode_characters);

See the example below of using this way to understand it more clearly.

Generally you will use the first method to create the strings.

An example of a C# string

Following is a simple example of creating a string. We will create a string by using the System.String class alias and display it. Click the link or image below to see online demo:

A string example by class constructor

Following example creates a string by using the String class constructor. See example by clicking the link below:

Example of string length property

As we mentioned, strings have its properties and methods. One of the useful property is the length.

The length property returns the number of characters of the current string. Following example shows how to use the string length property.

A few string methods and examples

The string class has very useful methods to work with string operations in C# programming. Now, let us look at a few useful methods of the String class.

String split method example

The string split method returns an array of the broken string. The specified string is broken by a given Unicode character. Following example shows how to use the string split method.

In this example, we have created a string and after that, we will break it by a space character. As such it returns an array, for understanding purpose we will display a specific array element of broken string as well as the whole array by using the foreach loop.

You can see, first we displayed 2nd index element and then used the foreach loop to display whole string, broken by using the split method.

The string replace method example

The string replace C# method is used to replace the given Unicode character(s) in the specified string to matched existing character(s). The replace method returns a string which is replaced by the given characters.

To make it clearer, see the following example of using string replace. In this example, we will replace a word “tutorial” to “Tutorial” by using the replace method.

You can see, both strings are displayed, before and after using string replace method.

C# trim method example

The C# string trim method removes all leading and trailing spaces in the specified string. See the following example of using the trim C# method where we created a string with the leading and trailing spaces.

After that, we displayed string before and after using the trim method.

You can see, all spaces from left and right are removed by using the trim method.

C# IndexOf method example

The IndexOf method is used to search in the string. Basically, IndexOf method finds the first occurrence of the given character and returns position in the integer value, based at zero index.

The following example will make it clear what actually IndexOf method does.

You can see, we used ‘s’ in IndexOf method and it returned 3 in the output. As such, IndexOf returns zero-based index value, so the first occurrence of ‘s’ letter is 3rd.

The string compare method

The C# string compare method is used to compare two given strings. The string compare method returns 0 if strings are matched and 1 if the strings are not matched.

Following example shows how to use it.

You can see, as both the strings are same, it returned 0 value.

You can also use CompareTo method as shown below.

The string CompareTo method

You can also use C# CompareTo method to compare two strings. The CompareTo method also returns 0 if the match is true, otherwise, it returns 1 (in case strings are not matched). See the example below to see how CompareTo method works.

You can see, as both strings are matched so CompareTo method returned 0.

The Equals method

The C# Equals method of strings also compares two strings. The specified string is matched with a given string and equals C# method returns true or false if the strings are matched or not.

See the following example of using the equals method.

The clone method of the string makes the clone of a string. See the example of using the clone method.

C# contains method example

The contains method checks if the given string value exists in specified string or not. The contains method returns the Boolean value, true or false. The returned value is true if the match is found otherwise false will be returned.

As you can see, we checked whether the word “tutorial” exists or not in the specified string. As it exists, the if condition evaluated as true after using the string contains method.

The GetHashCode method example

The C# GetHashCode method returns the hashcode of the given string. Following example shows using the GetHashCode method.

The join method example

The C# join method joins all elements of the array by a given separator. For example, a comma or space etc.

The following example adds a space between each array element of the string. Later, we added a comma separator by using the join method.

Was this article helpful?

Related Articles

Leave A Comment?