C# 如何检查字符串中每个字符的数据类型?
我对C#还不熟悉,所以预计会有一些错误。如有任何帮助/指导,将不胜感激 我想将字符串的可接受输入限制为:C# 如何检查字符串中每个字符的数据类型?,c#,C#,我对C#还不熟悉,所以预计会有一些错误。如有任何帮助/指导,将不胜感激 我想将字符串的可接受输入限制为: a-z A-Z 连字符 时期 如果字符是字母、连字符或句点,则接受该字符。其他任何操作都将返回错误 到目前为止,我掌握的代码是 string foo = "Hello!"; foreach (char c in foo) { /* Is there a similar way To do this in C# as I am basing th
- a-z
- A-Z
- 连字符
- 时期
string foo = "Hello!";
foreach (char c in foo)
{
/* Is there a similar way
To do this in C# as
I am basing the following
Off of my Python 3 knowledge
*/
if (c.IsLetter == true) // *Q: Can I cut out the == true part ?*
{
// Do what I want with letters
}
else if (c.IsDigit == true)
{
// Do what I want with numbers
}
else if (c.Isletter == "-") // Hyphen | If there's an 'or', include period as well
{
// Do what I want with symbols
}
}
我知道这是一套相当糟糕的代码
在写这篇文章时,我有一个想法:
是否可以创建一个允许的字符列表并对照该列表检查变量
比如:
foreach (char c in foo)
{
if (c != list)
{
// Unaccepted message here
}
else if (c == list)
{
// Accepted
}
}
提前谢谢 您可以使用正则表达式在单行中执行此操作:
Regex.IsMatch(myInput, @"^[a-zA-Z0-9\.\-]*$")
可以使用正则表达式在单行中执行此操作:
Regex.IsMatch(myInput, @"^[a-zA-Z0-9\.\-]*$")
使用
正则表达式轻松完成:
using System.Text.RegularExpressions;
var isOk = Regex.IsMatch(foo, @"^[A-Za-z0-9\-\.]+$");
概述:
match from the start
| set of possible matches
| |
|+-------------+
|| |any number of matches is ok
|| ||match until the end of the string
|| |||
vv vvv
^[A-Za-z0-9\-\.]+$
^ ^ ^ ^ ^
| | | | |
| | | | match dot
| | | match hyphen
| | match 0 to 9
| match a-z (lowercase)
match A-Z (uppercase)
使用正则表达式轻松完成:
using System.Text.RegularExpressions;
var isOk = Regex.IsMatch(foo, @"^[A-Za-z0-9\-\.]+$");
概述:
match from the start
| set of possible matches
| |
|+-------------+
|| |any number of matches is ok
|| ||match until the end of the string
|| |||
vv vvv
^[A-Za-z0-9\-\.]+$
^ ^ ^ ^ ^
| | | | |
| | | | match dot
| | | match hyphen
| | match 0 to 9
| match a-z (lowercase)
match A-Z (uppercase)
可以使用Regex.IsMatch函数并指定正则表达式
或者手动定义所需的字符。大概是这样的:
string foo = "Hello!";
char[] availableSymbols = {'-', ',', '!'};
char[] availableLetters = {'A', 'a', 'H'}; //etc.
char[] availableNumbers = {'1', '2', '3'}; //etc
foreach (char c in foo)
{
if (availableLetters.Contains(c))
{
// Do what I want with letters
}
else if (availableNumbers.Contains(c))
{
// Do what I want with numbers
}
else if (availableSymbols.Contains(c))
{
// Do what I want with symbols
}
}
可以使用Regex.IsMatch函数并指定正则表达式
或者手动定义所需的字符。大概是这样的:
string foo = "Hello!";
char[] availableSymbols = {'-', ',', '!'};
char[] availableLetters = {'A', 'a', 'H'}; //etc.
char[] availableNumbers = {'1', '2', '3'}; //etc
foreach (char c in foo)
{
if (availableLetters.Contains(c))
{
// Do what I want with letters
}
else if (availableNumbers.Contains(c))
{
// Do what I want with numbers
}
else if (availableSymbols.Contains(c))
{
// Do what I want with symbols
}
}
可能的解决办法
您可以使用CharUnicodeInfo.getunicodecegory(char)
方法。它返回字符的unicodegegory
。以下unicode类别可能是您要查找的:
unicodecegory.DecimalDigitNumber
unicodecegory.LowercaseLetter
和unicodecegory.UppercaseLetter
例如:
string foo = "Hello!";
foreach (char c in foo)
{
UnicodeCategory cat = CharUnicodeInfo.GetUnicodeCategory(c);
if (cat == UnicodeCategory.LowercaseLetter || cat == UnicodeCategory.UppercaseLetter)
{
// Do what I want with letters
}
else if (cat == UnicodeCategory.DecimalDigitNumber)
{
// Do what I want with numbers
}
else if (c == '-' || c == '.')
{
// Do what I want with symbols
}
}
其他问题的答案
我可以删掉==真实部分吗?:
是的,您可以剪切==true
部分,这在C中不是必需的#
如果有“或”,也包括句点。
要创建或表达式,请使用“barbar”(| |)运算符,就像我在上面的示例中所做的那样。可能的解决方案
您可以使用CharUnicodeInfo.getunicodecegory(char)
方法。它返回字符的unicodegegory
。以下unicode类别可能是您要查找的:
unicodecegory.DecimalDigitNumber
unicodecegory.LowercaseLetter
和unicodecegory.UppercaseLetter
例如:
string foo = "Hello!";
foreach (char c in foo)
{
UnicodeCategory cat = CharUnicodeInfo.GetUnicodeCategory(c);
if (cat == UnicodeCategory.LowercaseLetter || cat == UnicodeCategory.UppercaseLetter)
{
// Do what I want with letters
}
else if (cat == UnicodeCategory.DecimalDigitNumber)
{
// Do what I want with numbers
}
else if (c == '-' || c == '.')
{
// Do what I want with symbols
}
}
其他问题的答案
我可以删掉==真实部分吗?:
是的,您可以剪切==true
部分,这在C中不是必需的#
如果有“或”,也包括句点。
要创建或表达式,请使用“barbar”(| |)运算符,就像我在上面的示例中所做的那样。您可以使用Regex.IsMatch和“^[a-zA-Z.]*$”来检查有效字符
string foo = "Hello!";
if (!Regex.IsMatch(foo, "^[a-zA-Z_\.]*$"))
{
throw new ArgumentException("Exception description here")
}
除此之外,您可以创建一个字符列表,并使用string.Contains方法检查它是否正常
string validChars = "abcABC./";
foreach (char c in foo)
{
if (!validChars.Contains(c))
{
// Throw exception
}
}
另外,您不需要在if行中检查==true/false。下面两个表达式相等
if (boolvariable) { /* do something */ }
if (boolvariable == true) { /* do something */ }
可以将Regex.IsMatch与“^[a-zA-Z.]*$”一起使用以检查有效字符
string foo = "Hello!";
if (!Regex.IsMatch(foo, "^[a-zA-Z_\.]*$"))
{
throw new ArgumentException("Exception description here")
}
除此之外,您可以创建一个字符列表,并使用string.Contains方法检查它是否正常
string validChars = "abcABC./";
foreach (char c in foo)
{
if (!validChars.Contains(c))
{
// Throw exception
}
}
另外,您不需要在if行中检查==true/false。下面两个表达式相等
if (boolvariable) { /* do something */ }
if (boolvariable == true) { /* do something */ }
每当你有类似的东西的集合,一个数组,一个列表,一个字符串,无论什么,你都会在集合的定义中看到它实现了IEnumerable
公共类字符串:…,IEnumerable
这是一个字符。这意味着你可以问全班同学:“给我你的第一个T”,“给我你的下一个T”,“给我你的下一个T”等等,直到没有更多的元素
这是所有Linq的基础。Ling有大约40个作用于序列的函数。如果你需要对同一项目的顺序做一些事情,考虑使用LINQ.
LINQ中的函数可以在类Enumerable中找到。函数之一是Contains。您可以使用它来确定序列是否包含字符
char[]允许chars=“abcdefgh…XYZ.-”.tocharray()
现在您有了一个允许的字符序列。假设您有一个字符x,并想知道是否允许x:
char x = ...;
bool xIsAllowed = allowedChars.Contains(x);
现在假设您没有一个字符x,而是一个完整的字符串,并且您只需要该字符串中允许的字符:
string str = ...
var allowedInStr = str
.Where(characterInString => allowedChars.Contains(characterInString));
如果你要做很多事情,考虑花一些时间来熟悉LINQ:
每当你有类似的东西的集合,一个数组,一个列表,一个字符串,无论什么,你都会在集合的定义中看到它实现了IEnumerable
公共类字符串:…,IEnumerable
这是一个字符。这意味着你可以问全班同学:“给我你的第一个T”,“给我你的下一个T”,“给我你的下一个T”等等,直到没有更多的元素
这是所有Linq的基础。Ling有大约40个作用于序列的函数。如果你需要对同一项目的顺序做一些事情,考虑使用LINQ.
LINQ中的函数可以在类Enumerable中找到。函数之一是Contains。您可以使用它来确定序列是否包含字符
char[]允许chars=“abcdefgh…XYZ.-”.tocharray()
现在您有了一个允许的字符序列。假设您有一个字符x,并想知道是否允许x:
char x = ...;
bool xIsAllowed = allowedChars.Contains(x);
现在假设您没有一个字符x,而是一个完整的字符串,并且您只需要该字符串中允许的字符:
string str = ...
var allowedInStr = str
.Where(characterInString => allowedChars.Contains(characterInString));
如果你要做很多事情,考虑花一些时间来熟悉LINQ:
您可能需要查看常规表达式是的,您可以删除==true
。您还希望c=='-'
测试连字符。如果你有一个<代码