I'm trying :
1 string pal = "Juan 1David 1Correa";
2 StringBuilder sb = new StringBuilder(pal);
3 Console.writeline( sb.ToString(0,9) );
4 Console.writeline( sb.ToString(10,14) );
5 Console.writeline( sb.ToString(15,26) );
But in the 4 line It throws an exception.
Why?
The second argument to StringBuilder.ToString(int, int)
represents the length of the desired sub-string, not its end-index.
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
For example, the last statement should probably be:
Console.WriteLine(sb.ToString(15, 12));
On another note, If all you want is to get sub-strings from the original string, you could just use the String.Substring(int, int)
method.
Second parameter is length, so it should be
Console.writeline( sb.ToString(10,5) );
The docs clearly state that ArgumentOutOfRangeException
will be thrown when "The sum of startIndex and length is greater than the length of the current instance."
Second parameter is length but not a last index. So in your case 15+26 = 41 which is out of the bounds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With