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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of whatis similar to Substring of java if index is higher then string in c#? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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# ?

Answer

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.

We are here to answer your question about whatis similar to Substring of java if index is higher then string in c#? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji