First look at the Syntax:
String. substring (INT index, int length)
Index: Start position, starting from 0
Length: the length of the substring you want to obtain
Example:
1 using system; 2 using system. collections. generic; 3 using system. text; 4 5 namespace str_sub 6 {7 class Program 8 {9 static void main (string [] ARGs) 10 {11 string mystring = "Hello word! "; 12 // substring () in C # There are two overloaded functions 13 // The following example 14 string substring1 = mystring respectively. substring (0); 15 // If the input parameter is a long integer and greater than or equal to 0, 16 // the start point is the long integer, 17 // all the remaining strings are taken after the interception. 18 // If the input value is less than // The system will throw an argumentoutofrange exception 20 // indicates that the parameter range is 21 string substring2 = mystring. substring (0, 5); 22 // If two long integer parameters are input, 23 // The first parameter is the starting position of the original string 24 // The last parameter is the length of the substring 25 // The exception 26 27 appears if not met. writeline (substring1); 28 console. writeline (substring2); 29 console. readline (); 30} 31} 32}
Program output result:
Hello word!
Hello