public int ClosestIndexOf(string input, char value, int startIndex)
if (startIndex > input.Length)
throw new IndexOutOfRangeException("Start index was outside the length of input string.");
int below = input.LastIndexOf(value, startIndex - 1);
int above = input.IndexOf(value, startIndex);
var mid = (below + above) / 2;
Console.WriteLine(string.Format("StartIndex: {0}, Below: {1}, Above: {2}, Mid: {3}, Result: {4}", startIndex, below, above, mid, result));
string test = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Console.WriteLine("0 1 2 3 4 5 ");
Console.WriteLine("01234567890123456789012345678901234567890123456789012345");
Console.WriteLine("————————————————————————————————————————————————————————");
Console.WriteLine("————————————————————————————————————————————————————————");
ClosestIndexOf(test, ' ', 30);
ClosestIndexOf(test, ' ', 35);
ClosestIndexOf(test, ' ', 50);
ClosestIndexOf(test, ' ', 19);
ClosestIndexOf(test, 'x', 30);
ClosestIndexOf(test, 'a', 45);
ClosestIndexOf(test, 'a', 20);
ClosestIndexOf(test, ' ', 60);