需要帮助将代码从JS转换为C#

时间:2016-10-01 15:53:00

标签: javascript c#

这是Js代码:

n = o.length,
i = "";
for (e = 0; n > e; ++e) e % 3 === 0 && (i += o.substring(e, e + 1));

和C#代码是

int  n = newTemp.Length;
string final = "";
for (int e = 0; n > e; ++e)
{
     if( e%3==0 )
       {
          final += newTemp.Substring(e, e + 1);
       }
}

但是C#的代码没有给出与JS相同的结果。

2 个答案:

答案 0 :(得分:1)

c#substring与js substring不同。

在js:

text.substring(startIndex, endIndex);

more details

在C#中

text.Substring(startIndex, subtextLength);

More Details

答案 1 :(得分:0)

你的C#版本的问题是JS substring采用开始索引结束索引,其中C#substring采用开始索引 子串长度

另外,对于你的代码正在做什么(只接受一个字符),做这样的事情可能更好:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

(对变量名称和for循环的更改只是为了使事情更具可读性。)