public class TestSubString
private static string SubString(string value, int startIndex, int endIndex)
if (value == null) throw new ArgumentNullException();
if (endIndex > value.Length) throw new IndexOutOfRangeException("End index must be less than or equal to the length of the string.");
if (startIndex < 0 || startIndex > value.Length + 1) throw new IndexOutOfRangeException("Start index must be between zero and the length of the string minus one");
if (startIndex >= endIndex) throw new ArgumentOutOfRangeException("Start index must be less then end index");
var length = endIndex - startIndex;
return value.Substring(startIndex, length + 1);
public static void Main(string[] args)
Console.WriteLine(SubString("jumped", -1, 4));
catch (IndexOutOfRangeException)
Console.WriteLine("<error>");