PreviousNext

Other String Handling Operations

You can specify one character or a range of characters in a string using string index and string range. These commands would be useful for extracting information from a string of predictable length.

The string index command has one argument that is the position of one character (counting from left to right beginning with character zero) to be extracted from the string. The string range command includes two arguments that are the positions of the leftmost and rightmost characters to be included in the range. The following example illustrates one use of the string range command.

dcecp> string range {The quick brown fox} 4 9
quick
dcecp>

You can determine whether one string is lexicographically (alphabetically) greater than, less than, or equal to another string using string compare. Generally, this operation performs a byte comparison of ASCII codes that make up the string.

Count the number of characters in a string using the string length command. For instance:

dcecp> string length "The quick brown fox"
19
dcecp>

Convert characters between upper and lower case using the string toupper and string tolower commands. For example:

dcecp> string toupper "The quick brown fox"
THE QUICK BROWN FOX
dcecp>

Trim specific characters from a string using the string trim command. Remove the leftmost or rightmost characters from a string using the string trimleft and string trimright commands.

You can perform pattern matching operations in any of several ways. Invoke "glob" style pattern matching with the string match command. This mimics the glob pattern matching capabilities available in csh, returning a 1 for a match and a 0 for no match. More flexible regular expression pattern matching (like that found in egrep) can be performed using regexp command. You can extend this operation to perform regular expression substitution using the regsub command.

The following example illustrates the use of the regsub command. The first argument specifies the search pattern. The second argument is the string to search. The third argument specifies the replacement pattern. The last argument is a variable into which regsub places the new string. The command returns a 0 if no substitution occurs and a 1 if substitution does occur.

dcecp> regsub brown "The quick brown fox" blue color
1
dcecp> puts $color
The quick blue fox