Skip to content

Instantly share code, notes, and snippets.

@qjnz
Created July 25, 2012 06:22
Show Gist options
  • Save qjnz/3174714 to your computer and use it in GitHub Desktop.
Save qjnz/3174714 to your computer and use it in GitHub Desktop.
Get chars between begin and end chars
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
foreach (var c in GetCharsInBetween('b', 'x'))
Console.WriteLine(c);
Console.Read();
}
private static IEnumerable<char> GetCharsInBetween(char begin, char end)
{
if (begin > end)
throw new ArgumentException("invalid arguments.");
begin = char.ToLower(begin);
end = char.ToLower(end);
while (begin < end - 1)
{
yield return NextChar(begin);
begin++;
}
}
private static char NextChar(char c)
{
return Convert.ToChar((int)c + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment