This version is out of date, covering development from v7.0.0 to v7.5.6. It is maintained here only for inbound reference links from elsewhere. It is no longer actively updated.

Jump to the current version of aTbRef

Tinderbox 7 Icon

String.substr(startN[,lengthN])


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Altered: 

 Function   [other Function type actions]

 Item   [operators of similar scope]

 Data manipulation   [other Data manipulation operators]

 Baseline

 7.5.3


String.substr(startN][lengthN])

This operator allows extraction of a substring from a string attribute. The source string is not affected.

$MyString.substr(startN) 

returns the substring of $MyString beginning startN characters from the beginning. The numbers for startN and lengthN are 0-based. i.e. zero is character position #1. A negative startN value counts back from the end of the string .Negative values are 1-based, i.e. the -1 represents the last character in the string.

In the examples below assume $MyString's value is "Hello World". Examples:

"abcde".substr(2) returns "cde"

"abcde".substr(-2) returns "de"

$MyString.substr(6) returns "World"

If the string does not contain at least startN characters, the empty string is returned.

A second argument lengthN specifies the length of the returned string. If unspecified, the entire remaining string is returned.

"abcde".substr(2,2) returns "cd"

"abcde".substr(-3,2) returns "cd"

$MyString.substr(0,5) returns "Hello"

Besides strings and string literals, this operator can also be used on other attribute data types that are string-like, URL, File, etc. Although the operator also works on lists/sets, the source data is all the values as a single semi-colon-joined string literal so there is less point in its use in this context.

Trimming leading/trailing quotes

As Tinderbox cannot escape quotes, String.replace() cannot be used for stripping single/double quotes enclosing a string. But this will work:

$MyString=$MyString.substr(1,($MyString.size-2)) 

Also:

$Text=$Text.substr(1,($Text.size-2)) 

Bear in mind this is a brute-force change and simply deletes the first and last character of the referenced string. There is no test of what the character might be. So, use this technique only when sure that you wish to strip both first and last characters, a quote-enclosed string being a common example. For leading/trailing white space String.replace() offers a more flexible solution.

From v7.5.3, this functions respects existing rich text styling.