C# 如何按字母顺序找出下一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1026220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out next character alphabetically?
提问by MAC
How we can find out the next character of the entered one. For example, if I entered the character "b" then how do I get the answer "c"?
我们如何找出输入的下一个字符。例如,如果我输入字符“b”,那么我如何得到答案“c”?
采纳答案by rogeriopvl
Try this:
尝试这个:
char letter = 'c';
if (letter == 'z')
nextChar = 'a';
else if (letter == 'Z')
nextChar = 'A';
else
nextChar = (char)(((int)letter) + 1);
This way you have no trouble when the char is the last of the alphabet.
这样,当字符是字母表的最后一个时,您就没有问题了。
回答by Hemant
How about:
怎么样:
char first = 'c';
char nextChar = (char)((int) first + 1);
回答by Colin Pickard
Perhaps the simplest way is a little function and an array of the 26 chars. Then you can decide what you want to return for 'z'.
也许最简单的方法是一个小函数和一个 26 个字符的数组。然后你可以决定你想为'z'返回什么。
回答by Richard
Convert the character to a number, increment the number and then convert back.
将字符转换为数字,增加数字,然后再转换回来。
But consider what will happen for "z" or "á" (Latin Small Leter A with Acute).
但是考虑一下“z”或“á”(带有Acute的拉丁小写字母A)会发生什么。
回答by Nakul Chaudhary
need to just add 1 in character you get next character. It works on ASCII values.
只需在字符中添加 1 即可获得下一个字符。它适用于 ASCII 值。
回答by erikkallen
How does ? sort? In German (I think) it should sort after a, but in Swedish it should come after ?, which in turn is after z. This is not a trivial question, unless you restrict yourself to English.
如何 ?种类?在德语中(我认为)它应该在 a 之后排序,但在瑞典语中它应该在 ? 之后,而 ? 又在 z 之后。这不是一个微不足道的问题,除非你限制自己使用英语。
回答by JoshL
Note that a char will implicitly cast to an int. Here's a simplified solution:
请注意,char 将隐式转换为 int。这是一个简化的解决方案:
char incrementCharacter(char input)
{
return (input == 'z'? 'a': (char)(input + 1));
}
回答by Moisey Oysgelt
This Change value useful for Excel application to find previous column
此更改值对于 Excel 应用程序查找上一列很有用
public static string PrevExecelColumn( string s)
{
s = s.ToUpper();
char[] ac = s.ToCharArray();
int ln = ac.Length;
for (int i = ln - 1; i > -1; i--)
{
char c = ac[i];
if (c == 'A')
{
ac[i] = 'Z';
continue;
}
ac[i] = (char)(((int)ac[i]) - 1);
break;
}
s = new string(ac);
return s;
}
回答by Daniel
Try this :
尝试这个 :
public string GetNextAlphabetLetter(int indice) {
return ((char)('A' + indice)).ToString();
}