whatis similar to Substring of java if index is higher then string in c#?

in java the substring function(index,lenght) the index can be higher then lenght

System.out.print("BCDABCD".substring(3 , 7));

this output “ABCD”

i dont want this

Console.Writeline("BCDABCD".substring(3 , 4));

for ex if i want to generate this :

for i = 0 to 63 {Console.Writeline("BCDABCD".substring(i , i + 1 )));

how to do that in c# ?


The second parameter of String.substring(int, int) in Java isn’t the length – it’s the (exclusive) upper bound of the index. Whereas in .NET, the second parameter of String.Substring(int, int) really is a length.

So any time you call foo.substring(x, y) in Java, that’s equivalent to foo.Substring(x, y - x) in .NET… and you really need to take that into account.

It’s not really clear what you’re trying to do, but your loop wouldn’t work in Java either. If you really want to just get a single character from the string, just use the indexer along with %:

string text = "BCDABCD";
for (int i = 0; i < 63; i++)
    Console.WriteLine(text[i % text.Length]);

EDIT: Given the comment, I suspect you just want:

for (int i = 0; i < 63; i++)
    Console.WriteLine("BCDABCD".Substring(3 - (j & 3), 4));

… in other words, the length will always be 4.

Leave a Reply

Your email address will not be published. Required fields are marked *