Tuesday, 25 August 2015

String manipulations

One my friend asked me how to memorize these string manipulation functions in most of modern programming languages. Why the hek first argument in substring is zero based and second one is one based?

Everything will be easier if you realize that these offsets are not characters but something between characters. Imagine that characters in your string are separated by lines, and numbers are actually indexes of these separators rather than indexes of characters. The picture below will illustrate it well:

So, when you say “substring”, you ask computer to take a string between these separators and (a – b) will be amount of numbers to substring. If a==b, then you substring zero characters. The same about functions like insert, it inserts a string onto the separator, because you usually need to insert the string between characters, not onto them.

No comments:

Post a Comment