When it comes to string interception, we should first of all be substring functions, and today we will talk about substring functions.
1.public String Substring (int startIndex);
Retrieves a substring from this string. The substring starts at the specified character position (the startindex character) until the end of the string.
Class program { static void Main (string[] args) { string s1 = "Institution name/Teacher Name/course Type/course name"; Console.WriteLine (S1. Substring (0)); Console.WriteLine (S1. Substring (4)); Console.WriteLine (S1. Substring (S1. Length)); Console.WriteLine ("--------------------------------"); Console.WriteLine (S1. Substring (S1. length+1)); } }
2.public string Substring (int startIndex, int length);
retrieves a substring from this string. The substring starts at the specified character position, startindex, and has the specified length (length of the substring).
Class program { static void Main (string[] args) { string s1 = "Institution name/Teacher Name/course Type/course name"; Console.WriteLine (S1. Substring (0,0)); Console.WriteLine (S1. Substring (4,10)); Console.WriteLine (S1. Substring (S1. length,0)); Console.WriteLine ("--------------------------------"); Console.WriteLine (S1. Substring (4,s1. Length)); The string length is out of range //console.writeline (S1. Substring (S1. length,1)); The string length is out of range Console.WriteLine (S1. Substring (S1. length+1)); Start position cannot be greater than string length } }
Now we have such a demand, will "name of institution/Teacher name/course Type/Course name" This string, according to "/" respectively intercepted, see below we use SUBSTRING function how to implement.
Class Program {static void Main (string[] args) {string S1 = "Institution name/Teacher Name/course Type/course name"; int first = S1. IndexOf ("/") +1; Position of the first "/" int second = S1. IndexOf ("/", first + 1) + 1; The second "/" position is int third = S1. IndexOf ("/", second + 1) + 1; The third "/" Position Console.WriteLine ("First"/"Position:" + "); 7 Console.WriteLine ("Second '/' position:" + second); Console.WriteLine ("Third '/' position:" + third); startIndex1 int = 0; int length1 = first-1; 6--"Console.WriteLine" ("length1=" + length1) in the "name of the institution"; Console.WriteLine (S1. Substring (startindex1,length1)); Name of institution--starting from No. 0 position, 6 characters Console.WriteLine ("-------------------------------------------------"); int startIndex2 = first; 7 int length2 = (second-1)-first; 4--the position of "name" in "Teacher name"-the position of the first "/" Console.WriteLine ("Startindex2=" + StarTINDEX2); Console.WriteLine ("length2=" + length2); Console.WriteLine (S1. Substring (StartIndex2, length2)); Teacher name--starting from 7th position, 4 characters Console.WriteLine ("-------------------------------------------------"); int startIndex3 = second; length3 int = (third-1)-second; 4--the position of "type" in the "course type"-the position of the second "/" Console.WriteLine ("startindex3=" + startIndex3); Console.WriteLine ("length3=" + length3); Console.WriteLine (S1. Substring (StartIndex3, length3)); Course type-Starting from 12th position, 4 characters Console.WriteLine ("-------------------------------------------------"); int startIndex4 = third; Console.WriteLine ("startindex4=" + startIndex4); Console.WriteLine (S1. Substring (startIndex4)); Course name-Starting from the 17th position of the Console.WriteLine ("-------------------------------------------------"); } }
The SUBSTRING function implements string interception, and is commonly used with indexof functions. If we use the SUBSTRING function to achieve the above functions we need, logic is somewhat complex, the code is too many, accidentally easily error. So the next blog will teach you how to use other functions to simply implement the string interception function we want.
C # 's String intercept--substring