Is there a way to retrieve the part of a string between two indexes in C#? For example given this string,
Hello, world!
is there a convenient method to which I can pass "7" and "11" (the indexes of "w" and "d") and get "world"?
Note that I am aware of the String.Substring() method, and I know that I can do the following:
string s = "Hello, world!";
s.Substring(7, 11 - 7 + 1); // => "world"
I also realize that it would be trivial to create an extension method such as the following:
public static class Extensions {
public static string SubstringByIndexes(this string str, int start, int end) {
return str.Substring(start, end - start + 1);
}
}
However, I just wanted to make sure that there is no built-in way before I start lugging this method around in whatever projects I am working on.