substr

Returns the characters in a string beginning at the specified location through the specified number of characters.

Applies to

String

Syntax

substr(start, length)

Parameters

start

Location at which to begin extracting characters.

length

(Optional) The number of characters to extract.

Description

start is a character index. The index of the first character is 0, and the index of the last character is 1 less than the length of the string. substr begins extracting characters at start and collects length number of characters.

If start is positive and is the length of the string or longer, substr returns no characters.

If start is negative, substr uses it as a character index from the end of the string. If start is negative and abs(start) is larger than the length of the string, substr uses 0 is the start index.

If length is 0 or negative, substr returns no characters. If length is omitted, start extracts characters to the end of the string.

Example

Consider the following script:

str = "abcdefghij"
Console.Write("(1,2): ", str.substr(1,2))
Console.Write("(-2,2): ", str.substr(-2,2))
Console.Write("(1): ", str.substr(1))
Console.Write("(-20, 2): ", str.substr(1,20))
Console.Write("(20, 2): ", str.substr(20,2))

This script displays:

(1,2): bc
(-2,2): ij
(1): bcdefghij
(-20, 2): bcdefghij
(20, 2):

See also

String: substring