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.Substring(Int32, Int32) example
The following C# substring example shows how to extract a string by giving start index and specified length.
C# Substring left
The following example shows how to extract first n number of character from a specified String.
Substring Right
The following example shows how to extract last n number of character from a specified String.
Retrieve string before or after specified character in C#
The following example shows how to retrieve a string before a specified character or string.
The following example shows how to retrieve a string after a specified character or string.
Retrieve a substring between two strings
The following c# string example shows how to find the string between "substring" and "two".
ArgumentOutOfRangeException
Typically, an ArgumentOutOfRangeException results from 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 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 .
Conclusion
The Substring method is used to extract a portion of a string based on its starting index and optionally a specified length. It returns a new string containing the characters from the original string, starting at the specified index and extending for the specified length, or until the end of the string if the length is not specified.
- How to use C# string Clone
- How to use C# string Compare
- How to use C# string Concat
- How to use C# string Contains
- How to use C# string Copy
- How to use C# string CopyTo
- How to use C# string EndsWith
- How to use C# string Equals
- How to use C# string Format
- How to use C# string IndexOf
- How to use C# string Insert
- How to use C# string Length
- How to use C# string Split
- How to validate a string using TryParse in C#
- How to C# String Null
- Generate random strings, alphanumeric strings and numbers