Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C# string Substring



Substring in C# string Class returns a new string that is a substring of this string. This method is overloaded by passing the different number of arguments.

C# Substring() Method

  • String.Substring(Int32) method starts at a specified character position and continues to the end of the string.
  • String.Substring(Int32, Int32) method starts at a specified character position and has a specified length.

Substring(Int32) example

The following substring example shows how to extract a string using given only one parameter.

 
string str = null;
string retString = null;
str = "C# string substring examples";
retString = str.Substring(10);
MessageBox.Show(retString);

output

substring examples

String.Substring(Int32, Int32) example

The following C# substring example shows how to extract a string by giving the start index and specified length.

 
string str = null;
string retString = null;
str = "C# string substring examples";
retString = str.Substring(10,9);
MessageBox.Show(retString);

output

substring

C# Substring left

The following example shows how to extract the first n number of character from a specified String.

string str = null;
string retString = null;
str = "C# string substring left examples";
retString = str.Substring(0,10);
MessageBox.Show(retString);

output

C# string

Substring Right

The following example shows how to extract the last n number of character from a specified String.

 string str = null;
string retString = null;
str = "C# string substring right examples";
retString = str.Substring((str.Length-15), 15);
MessageBox.Show(retString);

output

 
right examples

Retrieve string before or after the specified character in C#

The following example shows how to retrieve a string before a specified character or string.

string str = "Retrieve string, before a specified character";
string retString = str.Substring(0, str.IndexOf(","));
MessageBox.Show(retString);

output

Retrieve string

The following example shows how to retrieve a string after a specified character or string.

string str = "Retrieve string, after a specified character";
string retString = str.Substring(str.IndexOf(",") + 2);
MessageBox.Show(retString);

output

after a specified character

Retrieve a substring between two strings

The following c# string example shows how to find the string between “substring” and “two”.

string str = "How to get a substring between two strings";
int start = str.IndexOf("substring") + "substring".Length;
int end = str.IndexOf("two") - start;
string retString = str.Substring(start, end);
MessageBox.Show(retString);

output

between

ArgumentOutOfRangeException

Typically, an ArgumentOutOfRangeException results from a programming error. It is thrown when a function is invoked, and at least one of the arguments passed to the method is not null and contains an invalid value that is not a member of the set of values expected for the argument.

In the C# String substring() method, this exception raised when Substring() is called with incorrect arguments. When you try to go beyond the string length or use an argument less than 0, you will get this exception.

C# substring Performance

C# Strings are immutable, so you can’t modify an existing string. If you try to change the string with the substring method, then you end up with an entirely new string. You can’t ever change an existing string. The operations you perform on a String frequently cause a new allocation of memory. As with all string methods, avoiding them when possible is often the best optimization.

When you execute this C# program, it will display “subtring” in the messagebox.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More