re: What happens when you IndexOf an empty string? VIEW POST

re: I suspect it originated as an implementation detail or performance optimization, and is staying around mostly for backwards compatibility. I can't...

If you were writing an IndexOf function, say it was a for-loop, you'd be comparing characters between the two strings. Comparing an empty string to any other value would be false. It would likely mean gettting to the end, not finding anything and then returning.

That said, I could see it returning 0 if the implement substring inside of IndexOf as as it might substring the length of the input and then compare two empty strings...

Still, it just feels wrong but I agree, even if people thought it was a problem, I doubt it will be fixed because of backwards compatibility.

code of conduct - report abuse