Whatis similar to java substring if index is higher than string in c #?

in java subscript function (index, length) index can be higher than length

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

this output is "abcd"

I do not want it

 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 it in c #?

0
source share
1 answer

The second parameter String.substring(int, int) in Java is not a length - it is the (exceptional) upper bound of the index. If in .NET the second parameter String.substring(int, int) really a length.

So anytime you call foo.substring(x, y) in Java, which is equivalent to foo.Substring(x, y - x) in .NET ... and you really need to keep that in mind.

It’s not entirely clear what you are trying to do, but your loop will not work in Java either. If you really want to just get one character from a 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.

+4
source

Source: https://habr.com/ru/post/1499809/


All Articles