Extracting parts of a string

From Conservapedia

Jump to: navigation, search

Extracting parts of a string works slightly different in major computer languages. Java and PHP take entirely different approaches, while JavaScript combines the best of each.

Java has the substring method int) which extracts a string beginning at the first argument, and ending just before the second argument. Each argument is an index into the string, taken as an offset from the first character.

PHP has the substr function, taking three arguments. The first is the string, and the next two are start and length. Like Java, the start argument is an offset from the first character.

The substring method in JavaScript works the same as in Java. The substr method in JavaScript is basically the same as in PHP, with JavaScript's two arguments corresponding to the last 2 arguments in PHP. However, in Javascript, a negative start is treated the same as zero.

Personal tools