How to extract a segment from a string in C#?

In C#, you can use the Substring method to extract a portion of a string. The Substring method takes two parameters: the first one is the starting position to extract (index starts from 0), and the second one is the length of the portion to extract. For example:

string str = "Hello World";
string subStr = str.Substring(6, 5);
Console.WriteLine(subStr); // 输出 "World"

In the example above, str.Substring(6, 5) means to extract a substring of length 5 starting from index 6.

bannerAds