简体   繁体   English

嵌套条件运算符?

[英]Nested conditional operators?

This seems stupid - must be me! 这看起来很愚蠢-一定是我!

Say I had a string filter["size]" = "g1g" this actually reads "greater" "1" "gb" 说我有一个字符串filter["size]" = "g1g"它实际上读为“ greater”“ 1”“ gb”

so 所以

I've done a test to see if "first" character is a character, and work out its greater I've done a test to see if "last" character is a character, and work out its gb, or mb, or whatever 我已经进行了测试以查看“第一个”字符是否为字符,并计算出更大的字符我已经进行了测试以查看“最后一个”字符是否为字符,并计算出其gb或mb或其他值

first and last are both boolean. first和last都是布尔值。

I then set x (cos I was getting stroppy) to length of filter["size"] which is a string. 然后,将x(cos我变得有点麻痹)设置为filter [“ size”]的长度,它是一个字符串。

However, I've come to copy the final number(s) in the middle. 但是,我要在中间复制最终编号。

Int64.Parse(filter["size"].Substring(first?1:0,last?(first?x-2:x-1):x));

And it doesnt like the nested inline ifs.. 而且它不喜欢嵌套的嵌入式ifs。

Substring(first?1:0,last?(first?x-2:x-1)); complains that its missing the :, which is right, but the instant you add it it complains 抱怨它缺少:,这是正确的,但是添加它的瞬间它抱怨

Error   1   The best overloaded method match for 'string.Substring(int, int)' has some invalid arguments

Im beginning to think I should just have parsed it with regexpr, but now Im curious as to why this isnt working. 我开始以为我应该用regexpr解析它,但是现在我对为什么它不起作用感到好奇。

So, irrelevant of there could be a ton of better ways, can anyone tell me why these nested inline ifs dont work? 因此,无关紧要的还有很多更好的方法,有人可以告诉我为什么这些嵌套的内联函数不起作用吗?

And it doesnt like the nested inline ifs.. 而且它不喜欢嵌套的嵌入式ifs。

The Conditional operator ?: is not complete in you Substring 条件运算符?:在您中不完整

I will break it down for you 我会为你分解

Substring(
           first
               ? 1
               : 0,
           last 
               ? (first ? x-2 : x-1 )
               : [something]  // this is the bit you're missing
);

Your code is perfectly compilable as you've described it , if horrendously readable. 如果您的代码可读性很强,那么您的代码可以完全按照您所描述的进行编译。 Filling out your blanks, I tried this: 填补您的空白,我尝试了以下方法:

int x = 6;
bool first = false;
bool last = false;
var filter = new Dictionary<string, string>();
filter.Add("size", "12345697");
var result = Int64.Parse(filter["size"].Substring(first?1:0,last?(first?x-2:x-1):x));

This code results in result being set to 123456 . 此代码导致将result设置为123456 No errors, no problems. 没有错误,没有问题。

There is something wrong with your surrounding code; 您的周围代码有问题; your ternary expressions are fine. 您的三元表达式很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM