Pascal substr equivalent

Michael Konečný picture Michael Konečný · Sep 3, 2012 · Viewed 21.4k times · Source

I was looking for a Pascal equivalent for (for example) the php's substr function, which works like this:

$new_string = substr('abcdef', 1, 3);  // returns 'bcd'

I've already found it, but I always take excessively long to do so, so I'm posting the answer for others like me to be able to easily find it.

Answer

Michael Konečný picture Michael Konečný · Sep 3, 2012

You can use the function copy. The syntax goes:

copy(string, start, length);

Strings in Pascal seem to be indexed starting from the 1, so the following:

s1 := 'abcdef';
s2 := copy(s1, 2, 3);

will result in s2 == 'bcd'.

Hope this helps someone.