DEV Community

Discussion on: What happens when you IndexOf an empty string?

lawrencejohnson profile image

I think in order to have an opinion, I'd have to understand why I'd be passing an empty string into IndexOf. The only reason I can think you might encounter that is if you are working with dynamic input either from a user or from an external source, but I can't think of a reason you'd be using IndexOf for that scenario. Can you give a real world example in which you would ever do this?

turnerj profile image
James Turner Author

I'm thinking things like a basic text search function where you might want to not only say that it is found but where in the document. Returning 0 gives the false impression it is found in the string.

The only time I feel like it could return 0 is if the string it was looking the index up in itself was empty. Even then, it is still searching for nothing and finding an index.