[英]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. 我有一个字符串,我需要检查此字符串是否包含给定列表中没有的任何字符。
Suppose i have this allowed chars new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' , '.'}
假设我已允许使用chars new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' , '.'}
If string is "54323.5" - this will be ok ! 如果字符串是“ 54323.5”-可以!
If string is "543g23.5" - this won't be ok since it contains "g" which is not in the list of my allowed chars. 如果字符串是“ 543g23.5”-这将是不可能的,因为它包含不在允许的字符列表中的“ g”。
An empty string is considered invalid. 空字符串被视为无效。
I am trying to achieve this by using "IndexOfAny()" but with no luck so far. 我正在尝试通过使用“ IndexOfAny()”来实现这一目标,但到目前为止还没有运气。 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;
}
Hope that you will come with a better solution :D. 希望您会找到更好的解决方案:D。
This can be done using a very simple pattern. 可以使用非常简单的模式来完成。 Regex.IsMatch(yourString, @"^[\\d.]+$");
^
is the beginning of the line ^
是行的开头
[\\d.]+
matches one or more characters (either .
or 0-9
) [\\d.]+
匹配一个或多个字符( .
或0-9
)
$
is the end of the line $
是行的结尾
Edit: This will also match .
编辑:这也将匹配.
If this behavior is not intended, then try using this ^(?=\\d)[\\d.]+$
如果不希望出现这种情况,请尝试使用此^(?=\\d)[\\d.]+$
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. string
类型实现IEnumerable<char>
,因此您可以使用LINQ All
方法来检查其所有字符是否都满足谓词。 In your case, the predicate is that each character is contained in the allowedChars
set, so you can use the Contains
method: 对于您的情况,谓词是每个字符都包含在allowedChars
集合中,因此可以使用Contains
方法:
private static bool CheckInvalidInput(string stringToCheck, IEnumerable<char> allowedChars)
{
return stringToCheck.All(allowedChars.Contains);
}
If your allowedChars
set gets large, you would want to convert it to a HashSet<char>
for better performance. 如果您的allowedChars
设置变大,则需要将其转换为HashSet<char>
以获得更好的性能。
Full example: 完整示例:
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);
}
}
If an array of allowed chars is dynamic, you can create procedure which would accept an array of allowed chars and construct pattern on the fly. 如果允许的字符数组是动态的,则可以创建将接受允许的字符数组并动态构造模式的过程。 Please, note that you have to escape certain chars in order to use in Regex: 请注意,您必须转义某些字符才能在Regex中使用:
static void TestRegex(char[] check_chars)
{
string[] inputs = { "54323.5", "543g23.5" };
var check_chars2 = check_chars.Select(c => Regex.Escape(c.ToString()));
string pattern = "^(" + string.Join("|", check_chars2) + ")+$";
foreach (string input in inputs)
{
WriteLine($"Input {input} does{(Regex.IsMatch(input, pattern) ? "" : " not")} match");
}
}
// Output:
// Input 54323.5 does match
// Input 543g23.5 does not match
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.