C# 检查字符是元音还是辅音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17764680/
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
Check if a character is a vowel or consonant?
提问by Square Ponge
Is there a code to check if a character is a vowel or consonant? Some thing like char = IsVowel? Or need to hard code?
是否有代码可以检查字符是元音还是辅音?像 char = IsVowel 这样的东西?还是需要硬编码?
case ‘a':
case ‘e':
case ‘i':
case ‘o':
case ‘u':
case ‘A':
case ‘E':
case ‘I':
case ‘O':
case ‘U':
采纳答案by p.s.w.g
You could do this:
你可以这样做:
char c = ...
bool isVowel = "aeiouAEIOU".IndexOf(c) >= 0;
or this:
或这个:
char c = ...
bool isVowel = "aeiou".IndexOf(c.ToString(), StringComparison.InvariantCultureIgnoreCase) >= 0;
Once you add international support for things like éèe???ê?e???и
etc. this string will get long, but the basic solution is the same.
一旦你为诸如此类的东西添加了国际支持,éèe???ê?e???и
这个字符串就会变长,但基本的解决方案是一样的。
回答by Ehsan
You can do something like this in.
你可以在里面做这样的事情。
private bool IsCharacterAVowel(char c)
{
string vowels = "aeiou";
return vowels.IndexOf(c.ToString(),StringComparison.InvariantCultureIgnoreCase) >= 0;
}
回答by Michael Bray
Here's a function that works:
这是一个有效的函数:
public static class CharacterExtentions
{
public static bool IsVowel(this char c)
{
long x = (long)(char.ToUpper(c)) - 64;
if (x*x*x*x*x - 51*x*x*x*x + 914*x*x*x - 6894*x*x + 20205*x - 14175 == 0) return true;
else return false;
}
}
Use it like:
像这样使用它:
char c = 'a';
if (c.IsVowel()) { // it's a Vowel!!! }
(Yes, it really works, but obviously, this is a jokeanswer. Don't downvote me. or whatever.)
(是的,它确实有效,但显然,这是一个笑话答案。不要贬低我。或其他什么。)
回答by NONE
Why not create an array of the vowels/consonants and check if the value is in the array?
为什么不创建一个元音/辅音数组并检查该值是否在数组中?
回答by MilindaD
You can use "IsVowel" as you wanted. However the only thing is there is likely no default C# library or function that already does this out of the box, well if this is what you wanted. You will need to write a util method for this.
您可以根据需要使用“IsVowel”。然而,唯一的问题是可能没有默认的 C# 库或函数已经开箱即用,如果这是您想要的。您需要为此编写一个 util 方法。
bool a = isVowel('A');//example method call
public bool isVowel(char charValue){
char[] vowelList = {'a', 'e', 'i', 'o', 'u'};
char casedChar = char.ToLower(charValue);//handle simple and capital vowels
foreach(char vowel in vowelList){
if(vowel == casedChar){
return true;
}
}
return false;
}
回答by Jukka K. Korpela
No. You need to define first what you regard as a vowel and as a consonant. For example, in English, “y” could be a consonant (as in “yes”) or a vowel (as in “by”). Letters like “é” and “ü” are probably vowels in all languages in which they are used, but it seems that you did not consider them at all. Primarily, you should define whyyou wish to classify letters as consonants and vowels.
不。您需要先定义您认为是元音和辅音的内容。例如,在英语中,“y”可以是辅音(如“yes”)或元音(如“by”)。像“é”和“ü”这样的字母在所有使用它们的语言中可能都是元音,但您似乎根本没有考虑它们。首先,您应该定义为什么要将字母分类为辅音和元音。
回答by imlokesh
You can use the following extension method:
您可以使用以下扩展方法:
using System;
using System.Linq;
public static class CharExtentions
{
public static bool IsVowel(this char character)
{
return new[] {'a', 'e', 'i', 'o', 'u'}.Contains(char.ToLower(character));
}
}
Use it like:
像这样使用它:
'c'.IsVowel(); // Returns false
'a'.IsVowel(); // Returns true
回答by DavMar
Console.WriteLine("Please input a word or phrase:");
string userInput = Console.ReadLine().ToLower();
for (int i = 0; i < userInput.Length; i++)
{
//c stores the index of userinput and converts it to string so it is readable and the program wont bomb out.[i]means position of the character.
string c = userInput[i].ToString();
if ("aeiou".Contains(c))
{
vowelcount++;
}
}
Console.WriteLine(vowelcount);
回答by Raghu
return "aeiou".Any( c => c.Equals( Char.ToLowerInvariant( myChar ) ) );
回答by user3587709
Try this out:
试试这个:
char[] inputChars = Console.ReadLine().ToCharArray();
int vowels = 0;
int consonants = 0;
foreach (char c in inputChars)
{
if ("aeiou".Contains(c) || "AEIOU".Contains(c))
{
vowels++;
}
else
{
consonants++;
}
}
Console.WriteLine("Vowel count: {0} - Consonant count: {1}", vowels, consonants);
Console.ReadKey();