The Substring() method in C# is used to retrieve a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
Syntax
The syntax is as follows -
public string Substring (int begnIndex); public string Substring (int begnIndex, int len);
Above, begnIndex is the zero-based starting character position of a substring in this instance. The len parameter is the number of the substring to retrieve
Example
Let us now see an example -
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Katherine";
string str2 = "PQRS";
Console.WriteLine("String1 = "+str1);
Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant());
Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4));
Console.WriteLine("\nString2 = "+str2);
Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant());
Console.WriteLine("String2 Substring from index2 = " + str2.Substring(2));
}
}Output
String1 = Katherine String1 ToUpperInvariant = KATHERINE String1 Substring from index4 = erine String2 = PQRS String2 ToUpperInvariant = pqrs String2 Substring from index2 = RS
Example
Let us now see another example -
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Notebook";
string str2 = "Ultrabook";
Console.WriteLine("String1 = "+str1);
Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant());
Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4, 4));
Console.WriteLine("\nString2 = "+str2);
Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant());
Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5));
}
}Output
String1 = Notebook String1 ToUpperInvariant = NOTEBOOK String1 Substring from index4 = book String2 = Ultrabook String2 ToUpperInvariant = ultrabook String2 Substring from index2 = Ultra