GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# String - Substring() Method
Selected Reading
C# String - Substring() MethodThe C# String Substring() method is used to retrieve a substring from the current instance of a string, starting at a specified character position and continuing to the end of the string. If a specific length is given, the method returns the substring starting from the character position and extending to the given length. SyntaxFollowing are the syntax of the C# string Substring() method − Syntax with only startIndex − public string Substring (int startIndex); Syntax with both startIndex and length − public string Substring(int startIndex, int length); ParametersThis method accepts the following parameters −
Return ValueThis method returns a new string which is a substring of the current string's instance, starting at the specified position and having the specified length. Example 1: Extract a Substring From StringHere is a basic example of the Substring() method, where we extract a substring that starts with a specified index position and continues at the end of the current instance − using System;
class Program {
public static void Main() {
string text = "Hello, tpians!";
string Substring = text.Substring(3);
Console.Write("This is substring: "+ Substring);
}
}
OutputFollowing is the output − This is substring: lo, tpians! Example 2: Extract Substring within Specified LengthLet's look at another example. Here, we use the substring() method to retrieve a substring from a given starting position to a specified length. − using System;
class Program {
public static void Main() {
string text = "Hello, tpians!";
string Substring = text.Substring(3, 7);
Console.Write("This is substring: "+ Substring);
}
}
OutputFollowing is the output − This is substring: lo, tpi Example 3: Extract First N CharactersIn this example, we use the Substring() method to extract the first n character. To extract the first n character, we need to mention startIndex at '0' and then the length − using System;
class Program {
public static void Main() {
string text = "Hello, tutorialspoint";
string Substring = text.Substring(0, 10);
Console.Write("First n character: "+ Substring);
}
}
OutputFollowing is the output − True Example 4: Handle Out-of-Range ScenarioThe below example handles the out-of-range scenario. If the start index is greater than the length of the string or '-1', then we get the error − using System;
class Program {
public static void Main() {
string str = "Hi, tutorialspoint";
try {
string substring = str.Substring(20);
} catch (ArgumentOutOfRangeException ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
OutputFollowing is the output − Error: startIndex cannot be larger than length of string. Parameter name: startIndex csharp_strings.htm Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |