[英]RegEx to find if a string lies in a range of number strings
Here I have a strange issue, I'm trying to replicate a behavior found in powerbuilder, to C#. 在这里,我有一个奇怪的问题,我试图将在powerbuilder中发现的行为复制到C#。
Here is the choose case statement in powerbuilder 这是powerbuilder中的select case语句
CHOOSE CASE ls_char
CASE "_"
ls_char = "-"
CASE "0" to "9"
MESSAGEBOX("DIGIT")
END CHOOSE
Now here comes the issue: ls_char can be a string or a char but still powerbuilder finds if it's equivalent numeric value falls between "0" and "9". 现在出现问题了:ls_char可以是字符串或char,但是powerbuilder仍然会发现它的等效数值是否介于“ 0”和“ 9”之间。 We have even tried it by giving "0" TO "900" and giving ls_char = "300" and it pops the messagebox.
我们甚至通过将“ 0”赋予“ 900”并给出ls_char =“ 300”来进行尝试,它会弹出消息框。
Any guidance regarding having some regex to achieve this or any other solution would be greatly appreciated. 任何有关使用正则表达式来实现此解决方案或任何其他解决方案的指导都将不胜感激。
In PowerScript CHOOSE
statement doesn't compare "...equivalent numeric value..." , it just compares objects as they are (in this case strings, without any further processing). 在PowerScript
CHOOSE
语句中不比较“ ...等效数值...” ,它只是按原样比较对象(在这种情况下为字符串,无需任何进一步处理)。 "9" is greater than "0" as "999" is greater than "000" (you can try "11" is smaller than "2"). “ 9”大于“ 0”,因为“ 999”大于“ 000”(您可以尝试“ 11”小于“ 2”)。
From PowerScript reference manual documentation for expressionlist
parameter in CASE
you can see TO
clause accepts just values, conversion is usually made from testexpression
type to expressionlist
type(s) and not vice-versa. 从
CASE
expressionlist
参数的PowerScript参考手册文档中,您可以看到TO
子句仅接受值,通常是从testexpression
类型转换为expressionlist
类型,而不是相反。 It means that if you provide strings then it'll compare strings instead of numbers! 这意味着,如果您提供字符串,那么它将比较字符串而不是数字!
There is not an equivalent of CHOOSE
in C# and switch
statement can't be used like that (you can find it, for example, in VB.NET for Select statement). C#中没有
CHOOSE
的等效项,并且switch
语句不能那样使用(例如,您可以在VB.NET中的Select语句中找到它)。 Workaround is to use if
instead: 解决方法是使用
if
:
string text = ls_char.ToString();
if (text == "_")
ls_char = "-";
else if (text >= "0" && text <= "9")
MessageBox.Show("Digit");
To compare numbers what you have to do is to provide numbers in CASE
blocks: 要比较数字,您需要在
CASE
块中提供数字:
CHOOSE CASE long(ls_char)
CASE 0 TO 5
MESSAGEBOX("SMALLER")
CASE 6 TO 9
MESSAGEBOX("SMALL")
END CHOOSE
In this case ls_char (whatever is its type) is converted to a number and then compared to check if it's in requested range. 在这种情况下,ls_char(无论其类型如何)都将转换为数字,然后进行比较以检查其是否在请求的范围内。 Roughly in C# it's equivalent to this:
大致在C#中,它等效于此:
int number = Convert.ToInt32(ls_char);
if (number >= 0 && number <= 5)
MessageBox.Show("Smaller");
else if (number >= 6 && number <= 9)
MessageBox.Show("Small");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.