简体   繁体   English

使用C#获取两个字母(AE)之间的字母(ABCDE)

[英]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# 我需要在使用C#传递两个字母时将字母作为数组

For ex.: When i pass "AE" , i need to get the {A,B,C,D,E} as an array. 例如:当我通过"AE" ,我需要将{A,B,C,D,E}作为数组。 and passing "FJ" should return {F,G,H,I,J}. 并且传递"FJ"应该返回{F,G,H,I,J}。

The Enumerable class can create a range, which makes the looping simple: Enumerable类可以创建一个范围,这使得循环变得简单:

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. 注意: char值隐式转换为int ,因此在该方向上不需要转换。 You only have to convert the integers back to char. 您只需将整数转换回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: 如果要发送字母表以使用(处理语言差异),可以使用Substring来获取该字符串的一部分:

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 . result现在将包含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. 你应该添加一些逻辑来检查startandend实际上有一个Length为2,依此类推,但这应该是一个很好的起点。

If you want the char[] instead of the string representation, simply call result.ToCharArray() at the end. 如果你想要char[]而不是字符串表示,只需在结尾处调用result.ToCharArray()

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 请参阅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 "Å, Ä, Ö". Z和瑞典语之后的丹麦和挪威地方“Æ,Ø,Å”与“Å,Ä,Ö”相同。 Irish uses "ABCDEFGHILMNOPRSTU" as the alphabet, but does also use J, K, Q, V, W, X, Y & Z in loan words. 爱尔兰语使用“ABCDEFGHILMNOPRSTU”作为字母表,但在借词中也使用J,K,Q,V,W,X,Y和Z.

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, eg 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?). 因此,例如丹麦字母表中的字符串为“ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ”,而法语可以根据您的意愿包含连字符(但是您是否需要处理在不使用它们的情况下接收它们的可能性?)。

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++];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM