公式解释

Formula explanation

这是我第一次 post 来这里,所以这可能是我做错了 post。

我是菜鸟,希望有人能准确解释一下这些公式的作用。 我已经成功使用了但是我不是很懂

string[] split = new string[str.Length / 2 + (str.Length % 2 == 0 ? 0 : 1)];
split[x] = str.Substring(x * 2, x * 2 + 2 > str.Length ? 1 : 2);

我进行了很多搜索,但找不到任何与我真正相关的内容。

提前致谢!

假设 str 的内容值类似于 Whosebug

str.Length -> will be length of the above value i.e. 13

% is called Mod 

就像你将字符串长度 (13) 除以 2 得到 01 的平衡,在这种情况下它不会是 0.

% 2 == 0 ? 0 : 1)

这是一个三元运算符,你看看是什么意思here 如果 Mod 的值是 true 那么它应该 return 值 0 如果不是 return 值 1.

进入下一行代码。 substring(): 从此实例中检索子字符串。

str.Substring(x * 2, x * 2 + 2 > str.Length ? 1 : 2);

查找有关子字符串的更多信息 here 我再次假设 x 的值为 4。然后上面的行可能看起来像

str.Substring(4 * 2, 4 * 2 + 2 > 13 ? 1 : 2); //in our case it is 13.

这和普通数学没什么区别。上面一行还包括我上面提到的三元运算符。

希望我能给你一些关于代码的提示!