字段数据有 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
我是一名优秀的程序员,十分优秀!