How to extract a portion of a string in C#.

In C#, you can use the Substring method to extract a specific portion of a string. There are two different versions of the Substring method.

  1. Substring(int startIndex): Extracts the substring starting from the specified startIndex position to the end of the string.
  2. Extract a substring starting from the specified startIndex position with a length of ‘length’.

Here is an example of using the Substring method to extract a portion of a string:

string str = "Hello, World!";
string substring1 = str.Substring(7); // 从索引为7的位置开始截取,结果为"World!"
string substring2 = str.Substring(7, 5); // 从索引为7的位置开始,截取长度为5的子字符串,结果为"World"

In this example, str.Substring(7) will return a substring starting from index 7 to the end of the string, which is “World!”. On the other hand, str.Substring(7, 5) will return a substring starting from index 7 with a length of 5 characters, which is “World”.

bannerAds