如何检查字符串是否包含给定字符列表之外的字符 [英] How to check if a string contains chars that are outside of a given char list
问题描述
我有一个字符串,我需要检查此字符串是否包含不在给定列表中的任何字符.
I have a string, and I need to check if this string contains any chars that are not in a given list.
假设我允许使用此字符, new char [] {'0','1','2','3','4','5','6','7','8','9','.'}
Suppose i have this allowed chars new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' , '.'}
如果字符串为"54323.5"-没关系!
If string is "54323.5" - this will be ok !
如果字符串是"543g23.5"-这将是不可能的,因为它包含的"g"不在我允许的字符列表中.
If string is "543g23.5" - this won't be ok since it contains "g" which is not in the list of my allowed chars.
一个空字符串被认为是无效的.
An empty string is considered invalid.
我正在尝试通过使用"IndexOfAny()"来实现这一目标,但是到目前为止还没有运气.当然,将所有不允许的字符传递给此方法将不是解决方案.
I am trying to achieve this by using "IndexOfAny()" but with no luck so far. Of course passing all the unallowed chars to this method won't be a solution.
请注意,允许的字符列表可能会更改,并且基于列表更改来更改验证算法不被视为解决方案.
Please note that the list of the allowed chars may change and changing the validation algorithm based on the list change is not considered a solution.
对于那些问我尝试过的代码的人,这里是:
For you guys that asked me the code that I tried, here it is:
private bool CheckInvalidInput(string stringToCheck)
{
char[] allowedChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
var chars = Enumerable.Range(0, char.MaxValue + 1)
.Select(i => (char)i)
.ToArray();
var unallowedChars = chars.Except(allowedChars).ToArray();
bool validString = true;
if(stringToCheck.IndexOfAny(unallowedChars) != -1)
{
validString = false;
}
return validString;
}
希望您会提供更好的解决方案:D.
Hope that you will come with a better solution :D.
推荐答案
这很容易实现. string
类型实现了 IEnumerable< char>
,因此您可以使用LINQ All
方法来检查其所有字符是否都满足谓词.在您的情况下,谓词是每个字符都包含在 allowedChars
集中,因此您可以使用 Contains
方法:
This is straightforward to achieve. The string
type implements IEnumerable<char>
, so you can use the LINQ All
method to check that all its characters satisfy a predicate. In your case, the predicate is that each character is contained in the allowedChars
set, so you can use the Contains
method:
private static bool CheckInvalidInput(string stringToCheck, IEnumerable<char> allowedChars)
{
return stringToCheck.All(allowedChars.Contains);
}
如果您的 allowedChars
设置变大,则需要将其转换为 HashSet< char>
以获得更好的性能.
If your allowedChars
set gets large, you would want to convert it to a HashSet<char>
for better performance.
完整示例:
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
// var allowedChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' };
var allowedChars = "0123456789.";
Console.WriteLine(CheckInvalidInput("54323.5", allowedChars)); // True
Console.WriteLine(CheckInvalidInput("543g23.5", allowedChars)); // False
}
private static bool CheckInvalidInput(string stringToCheck, IEnumerable<char> allowedChars)
{
return stringToCheck.All(allowedChars.Contains);
}
}
这篇关于如何检查字符串是否包含给定字符列表之外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!