Quote:
Sorry I did not write a more error proof code, just didn't know I need to, since it works if you know what you are doing.
So does Substring [grin]
But seriously, learning to spot odd boundary conditions is a good habit to get into, but a vital one if you are going to write generic functions like that which will end up being used all over the place.
In general, if you think you've found a bug in something like System.String, you are almost certainly wrong. This code is independently tested in millions if not billions of lines of code all around the world. If Substring stopped working there would be a deluge of bug reports. Not to say it cannot happen, just that it is vanishingly unlikely.
If you do think you've found such a bug, you should write a minimal example which demonstrates it. Something like this:
using System;public class Example{ public static void Main(string [] args) { string html = "<html><head><title>Whatever</title></head><body>CONTENT</body></html>"; int start = html.IndexOf("<title>"); int end = html.IndexOf("</title>"); // Expected "Whatever" but is "Whatever</title></head><bod" Console.WriteLine(html.Substring(start + "<title>".Length, end)); }}
This immediately demonstrates that you are using the API incorrectly. In other examples, it will focus your mind on the problem and you might be able to solve it yourself. In the case where there is still an apparent bug, it gives us something to try out ourselves to confirm the behaviour diverges from the expected output.
Quote:
Errrr, I am wrong... I put the index of the last element as a lenth parameter which the substring takes. Rycross was right, I dind't understand the substring completely. Thank you for correcting me.
It was right there all along in the very first reply:
Quote:
You know that the function is actually substring(start, length)?