When I have a string that I want to cut into a new string from a certain Index to a certain Index, which function do I use?
If the string was:
ABCDEFG
This would mean retrieving BCD when the two indexes specified were 1 and 3.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If
endIndexpoints to the last character that you want to have included in the extracted substring:If
endIndexpoints to the first character following the desired substring (i.e. to the start of the remaining text):See String.Substring Method (Int32, Int32) for the official description on Microsoft Docs.
Since C# 8.0, in .NET Core and .NET 5+ only, you can use Indices and ranges
where the position at endIndex is excluded. This corresponds to my second example with
SubstringwhereendIndexpoints to the first character following the desired substring (i.e. to the start of the remaining text).If
endIndexis intended to point to the last character that you want to have included, just add one toendIndex: