开发者

Get the letters (ABCDE) between two letters (AE) using C#

I need to get the letters as an array on passing two 开发者_开发问答letters using C#

For ex.: When i pass "AE", i need to get the {A,B,C,D,E} as an array. and passing "FJ" should return {F,G,H,I,J}.


The Enumerable class can create a range, which makes the looping simple:

public static char[] CharactersBetween(char start, char end) {
  return Enumerable.Range(start, end - start + 1).Select(c => (char)c).ToArray();
}

Note: A char value converts implicitly into int, so there is no conversion needed in that direction. You only have to convert the integers back to char.

Edit:

If you want to send in the alphabet to use (to handle language differences), you can use Substring to get a part of that string:

public static char[] CharactersBetween(char start, char end, string alphabet) {
  int idx = alphabet.IndexOf(start);
  return alphabet.Substring(idx, alphabet.IndexOf(end) - idx + 1).ToCharArray();
}


Do you mean something like

char[] CharactersBetween(char start, char end)
{
    List<char> result = new List<char>();
    for (char i = start; i <= end; i++)
    {
        result.Add(i);
    }
    return result.ToArray();
}


This should work out well

string startandend = "AG";

string result= "";
for( char i = startandend[0]; i <= startandend[1]; i++){
   result += i;
}

result will now contain ABCDEFG.

You should probably add some logic to check if startandend actually have a Length of 2 and so on, but this should be a good starting block for you.

If you want the char[] instead of the string representation, simply call result.ToCharArray() at the end.


Use a loop with integer conversion with

System.Convert.ToInt32(Char);

and

System.Convert.ToChar(Int32);

see http://msdn.microsoft.com/en-us/library/system.convert_methods.aspx


Pretty simple if you use a fixed alphabet,

public static string ALPHABET = "ABCDEFGHIJKLMNOPWRSTUVWXYZ";
public static List<char> GetLetters(string firstLast)
{
  List<char> letters = new List<char>();
  int start = ALPHABET.IndexOf(firstLast[0]);
  int end = ALPHABET.IndexOf(firstLast[1]);
  for (int i = start; i <= end; i++)
  {
    letters.Add(ALPHABET[i]);
  }
  return letters;
}

Obviously add in your checks for various things, but it does the basic job.


You're going to have to reference whichever alphabet you want to use. English is easy enough as the letters happen to correspond to code-point order, French treats Œ and Æ as letters in their own right sometimes, and not others. Danish and Norwegian place "Æ, Ø, Å" after Z and Swedish does the same with "Å, Ä, Ö". Irish uses "ABCDEFGHILMNOPRSTU" as the alphabet, but does also use J, K, Q, V, W, X, Y & Z in loan words.

And those are relatively easy cases. So there's no one-size-fits-all.

The easiest way to pass an alphabet is to have a string that contains it. So, e.g. the Danish alphabet would have the string "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ" while French could either include the ligatures or not as you wish (but do you need to deal with the possibility of receiving them while not using them?).

This done:

public static IEnumerable<char> AlphabetRange(string alphabet, string alphaRange)
{
  if(alphaRange == null)
    throw new ArgumentNullException();
  if(alphaRange.Length < 2)
    throw new ArgumentException();
  int startIdx = alphabet.IndexOf(alphaRange[0]);
  int endIdx = alphabet.IndexOf(alphaRange[1]) + 1;
  if(startIdx == -1 || endIdx == 0)
    throw new ArgumentException();
  while(startIdx < endIdx)
    yield return alphabet[startIdx++];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜