gpt4 book ai didi

c# - switch 语句 - 验证子串

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:38 24 4
gpt4 key购买 nike

字段数据有 4 种可接受的值类型:

 j
47d (where the first one-two characters are between 0 and 80 and third character is d)
9u (where the first one-two characters are between 0 and 80 and third character is u)
3v (where the first character is between 1 and 4 and second character is v).

否则数据应被视为无效。

字符串数据 = readconsole();

验证此输入的最佳方式是什么?

我正在考虑结合使用 .Length 和 Switch 子字符串检查。

即。

if (data == "j")

else if (data.substring(1) == "v" && data.substring(0,1) >=1 && data.substring(0,1) <=4)
....
else
writeline("fail");

最佳答案

您可以使用匹配不同类型值的正则表达式:

^(j|(\d|[1-7]\d|80)[du]|[1-4]v)$

例子:

if (Regex.IsMatch(data, @"^(j|(\d|[1-7]\d|80)[du]|[1-4]v)$")) ...

正则表达式的解释:

^ matches the beginning of string
j matches the literal value "j"
| is the "or" operator
\d matches one digit
[1-7]\d matches "10" - "79"
80 matches "80"
[du] matches either "d" or "u"
[1-4] matches "1" - "4"
v matches "v"
$ matches the end of the string

关于c# - switch 语句 - 验证子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com