Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
To use `String.Substring()`, you’ll need to provide two arguments: the starting index and the length of the substring you want to retrieve. For example, if you wanted to retrieve the character at index 3 of the string “hello”, you would use the following code:
string myString = "hello";
char myChar = myString.Substring(3, 1);
In this example, we’re creating a new string called `myString` and setting its value to “hello”. We then use `Substring()` to retrieve a substring starting at index 3 with a length of 1. Finally, we access the first character of the resulting substring using the `` index.
It’s worth noting that `String.Substring()` is zero-indexed, meaning that the first character in a string is at index 0, not 1. So if you wanted to retrieve the first character of a string, you would use an index of 0.