Skip to main content

String Methods

In this article, we'll be exploring String Methods in C#. Strings are a significant part of any programming language, and C# is no exception. Strings in C# are essentially sequences of characters, and they come with numerous methods to manipulate and work with them.

What is a String?

A string in C# is an object of the System.String class. This class comes with various built-in methods to manipulate strings.

string example = "Hello, C# learners!";

In the code above, example is a string that contains the phrase "Hello, C# learners!".

Important String Methods

Let's delve into some of the critical methods provided by the String class in C#.

Length

The Length property returns the number of characters in a string.

string example = "Hello, C# learners!";
Console.WriteLine(example.Length); // Outputs: 19

Substring

The Substring method returns a new string that is a substring of the string. You can specify the start index and the number of characters to return.

string example = "Hello, C# learners!";
Console.WriteLine(example.Substring(7, 2)); // Outputs: C#

Replace

The Replace method replaces all occurrences of a specified substring with another substring.

string example = "Hello, C# learners!";
Console.WriteLine(example.Replace("C#", "Java")); // Outputs: Hello, Java learners!

ToUpper and ToLower

The ToUpper and ToLower methods convert all the characters in a string to upper case or lower case respectively.

string example = "Hello, C# learners!";
Console.WriteLine(example.ToUpper()); // Outputs: HELLO, C# LEARNERS!
Console.WriteLine(example.ToLower()); // Outputs: hello, c# learners!

Trim

The Trim method removes all leading and trailing whitespace from a string.

string example = "    Hello, C# learners!    ";
Console.WriteLine(example.Trim()); // Outputs: Hello, C# learners!

Split

The Split method splits a string into an array of substrings based on the characters in an array.

string example = "Hello, C# learners!";
string[] words = example.Split(' ');
foreach (var word in words)
{
Console.WriteLine(word);
}
// Outputs:
// Hello,
// C#
// learners!

Contains

The Contains method returns a value indicating whether a specified substring occurs within this string.

string example = "Hello, C# learners!";
Console.WriteLine(example.Contains("C#")); // Outputs: True

StartsWith and EndsWith

The StartsWith and EndsWith methods return a value indicating whether the beginning or end of this string instance matches the specified string.

string example = "Hello, C# learners!";
Console.WriteLine(example.StartsWith("Hello")); // Outputs: True
Console.WriteLine(example.EndsWith("!")); // Outputs: True

Conclusion

In this tutorial, we have covered different string methods in C#. Strings are an essential part of C# programming, and being able to manipulate and work with them is a crucial skill. Practice using these methods to become comfortable with handling strings in C#.

Remember, the best way to understand these methods is to use them in your code. So, start coding!