12.5.38    string

Description:   Manipulate and compare strings

Syntax:         string option arg ?arg ...?

          

Argument:     search or conversion option,  string variables

Returns:        elements extracted from string

See Also:       format, scan, list

 

Examples:         

SETVAL text1=[string index "See Spot run." 4]

#S

 

SETVAL text2=[string range "See Spot run." 4 8]

# Spot

 

SETVAL text3=[string range "See Spot run." 4 end]

# Spot run.

Search and compare strings. Search for a sub string with first or last returns the position of the first character of the sub string. The search begins at 0 for the first character in the input string. Returns -1 if no match was found.

 

SETVAL analog1=[string first th "The trains were thirty minutes late this past week"]

# analog1=16

 

SETVAL analog2=[string last th "The trains were thirty minutes late this past week"]

# analog2=36

 

Compare returns 0 if the strings match, -1 if the first string sorts before the second, and 1 if the first string sorts after the second.

 

SETVAL digital1=[string compare twelve thirteen]

# digital1=1

 

SETVAL digital2=[string compare twelve twelve

# digital2=0

 

Length, case conversion, and trimming

 

catch {

SETVAL text4=[string length "not too long"]

# text4=12

}

 

catch {

SETVAL text5=[string toupper "Hello World!"]

# text5=HELLO WORLD!

}

 

catch {

SETVAL text6=[string tolower "You are lucky winner 13!"]

# text6= you are lucky winner 13!

}

 

catch {

SETVAL text7=[string trim abracadabra abr]

# text7= cad

}

 

Note - a comma in the text string will cause string command to fail. For example, "Hello, World!" would not work.

 

string trim takes a string to trim and an optional set of trim characters and removes all instances of the trim characters from both the beginning and end of its argument string, returning the trimmed string as result. trimleft and trimright options work in the same way except they only remove the trim characters from the beginning or end of the string. The trim commands are mostly commonly used to remove excess white space; if no trim characters are specified, they default to the white space characters (space, tab, newline, carriage return, and form feed).

OPTIONS

String performs one of several string operations, depending on option. The  options are:

 

string compare string1 string2

Perform a character-by-character comparison of strings string1 and string2. Return -1, 0, or 1, depending on whether string1 is lexicographically less than, equal to, or greater than string2.

 

string first string1 string2

Search string2 for a sequence of characters that exactly match the characters in string1. If found, return the index of the first character in the first such match within string2. If not found, return -1.

 

string index string charIndex

Returns the charIndex’th character of the string argument. A charIndex of 0 corresponds to the

first character of the string. If charIndex is less than 0 or greater than or equal to the length of the string then an empty string is returned.

string last string1 string2

Search string2 for a sequence of characters that exactly match the characters in string1. If found, return the index of the first character in the last such match within string2. If there is no match, then return -1.

 

string length string

Returns a decimal string giving the number of characters in string.

 

string match pattern string

See if pattern matches string; return 1 if it does, 0 if it doesn’t. Matching is done in a fashion similar to that used by the C-shell. For the two strings to match, their contents must be identical except that the following special sequences may appear in pattern:

*                              Matches any sequence of characters in string, including a null string.

?                      Matches any single character in string.

[chars]           Matches any character in the set given by chars. If a sequence of the form x-y appears in chars, then any character between x and y, inclusive, will match.

\x                Matches the single character x

 

string range string first last

Returns a range of consecutive characters from string, starting with the character whose index is first and ending with the character whose index is last. An index of 0 refers to the first character of the string. An index of end (or any abbreviation of it) refers to the last character of the string. If first is less than zero then it is treated as if it were zero, and if last is greater than or equal to the length of the string then it is treated as if it were end. If first is greater than last then an empty string is returned.

 

string tolower string

Returns a value equal to string except that all upper case letters have been converted to lower case.

 

string toupper string

Returns a value equal to string except that all lower case letters have been converted to upper case.

 

string trim string ?chars?

Returns a value equal to string except that any leading or trailing characters from the set given by chars are removed. If chars is not specified then white space is removed (spaces, tabs, newlines, and carriage returns).

 

string trimleft string ?chars?

Returns a value equal to string except that any leading characters from the set given by chars are removed. If chars is not specified then white space is removed (spaces, tabs, newlines, and carriage returns).

 

string trimright string ?chars?

Returns a value equal to string except that any trailing characters from the set given by chars are removed. If chars is not specified then white space is removed (spaces, tabs, newlines, and carriage returns).

 

string wordend string index

Returns the index of the character just after the last one in the word containing character index of string. A word is considered to be any contiguous range of alphanumeric or underscore characters, or any single character other than these.

 

string wordstart string index

Returns the index of the first character in the word containing character index of string. A word is considered to be any contiguous range of alphanumeric or underscore characters, or any single character other than these.