java中的子字符串函数(index,lenght)索引可以高于lenght
System.out.print("BCDABCD".substring(3 , 7));
此输出“ABCD”
我不想要这个
Console.Writeline("BCDABCD".substring(3 , 4));
例如,如果我想生成这个:
for i = 0 to 63 {Console.Writeline("BCDABCD".substring(i , i + 1 )));
如何在 C# 中做到这一点?
最佳答案
String.substring(int, int)
的第二个参数在 Java 中不是长度 - 它是索引的(独占)上限。而在.NET中,String.Substring(int, int)
的第二个参数实际上是一个长度。
因此,任何时候您在 Java 中调用 foo.substring(x, y)
时,都相当于 .NET 中的 foo.Substring(x, y - x)
。 ..你确实需要考虑到这一点。
目前还不清楚你想要做什么,但你的循环在 Java 中也不起作用。如果您确实只想从字符串中获取单个字符,只需将索引器与 %
一起使用:
string text = "BCDABCD";
for (int i = 0; i < 63; i++)
{
Console.WriteLine(text[i % text.Length]);
}
编辑:鉴于评论,我怀疑您只是想要:
for (int i = 0; i < 63; i++)
{
Console.WriteLine("BCDABCD".Substring(3 - (j & 3), 4));
}
...换句话说,长度始终为 4。
关于c# - 如果索引高于c#中的字符串,什么类似于java的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18536161/