String truncation of C # -- Substring,

Source: Internet
Author: User

String truncation of C # -- Substring,
Speaking of string truncation, we should first talk about the substring function. Let's talk about the substring function today.


1. public String Substring (int startIndex );

Retrieve the substring from this string. The sub-string starts from the specified character position (startIndex character) until the end of the string.

Class Program {static void Main (string [] args) {string s1 = "institution name/instructor 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 );
Retrieve the substring from this string. The substring starts with the specified character position startIndex and has the specified length (the length of the substring ).

Class Program {static void Main (string [] args) {string s1 = "institution name/instructor 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 exceeds the range // Console. writeLine (s1.Substring (s1.Length, 1); // the string length exceeds the range Console. writeLine (s1.Substring (s1.Length + 1); // the start position cannot exceed the string length }}





Now we have such a requirement to extract the string "institution name/instructor name/course type/Course name", the following describes how to implement the Substring function.

Class Program {static void Main (string [] args) {string s1 = "institution name/instructor name/course type/Course name"; int first = s1.IndexOf ("/") + 1; // The first "/" position int second = s1.IndexOf ("/", first + 1) + 1; // The second "/" location int third = s1.IndexOf ("/", second + 1) + 1; // The third "/" location Console. writeLine ("location of the first '/':" + first); // 7 Console. writeLine ("Location of the second '/':" + second); // 12 Console. writeLine ("third '/' location:" + third); // 16 int startIndex1 = 0; int length1 = first-1; // 6 -- the "name" in "institution name" is displayed on the Console. writeLine ("length1 =" + length1); Console. writeLine (s1.Substring (startIndex1, length1); // The name of the institution to which it belongs. The name starts from the second position and starts with 6 characters. writeLine ("-----------------------------------------------"); int startIndex2 = first; // 7 int lengh2 = (second-1)-first; // 4 -- location of "name" in "instructor name"-location of the first "/" Console. writeLine ("startIndex2 =" + startIndex2); Console. writeLine ("lengith2. =" + lengith2.); Console. writeLine (s1.Substring (startIndex2, leng22.); // instructor name, which starts from the second position and contains four characters: Console. writeLine ("-----------------------------------------------"); int startIndex3 = second; // 12 int length3 = (third-1)-second; // 4 -- "course type" medium "type" location-second "/" location Console. writeLine ("startIndex3 =" + startIndex3); Console. writeLine ("length3 =" + length3); Console. writeLine (s1.Substring (startIndex3, length3); // The course type, which starts from the second position and contains four characters. writeLine ("-----------------------------------------------"); int startIndex4 = third; // 17 Console. writeLine ("startIndex4 =" + startIndex4); Console. writeLine (s1.Substring (startIndex4); // Course name -- Console starts from 17th locations. writeLine ("-------------------------------------------------");}}






The Substring function can intercept strings. It is generally used with the IndexOf function. If we use the Substring function to implement the functions we need above, the logic is somewhat complicated and there are too many codes, so it is easy to make mistakes if we are not careful. The next blog will teach you how to use other functions to simply implement the string truncation function we want.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.