Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
a := [2,3,5] paste( "the first three primes are", a )yields
the first three primes are [2 3 5]The []'s seen here in the string representation of the vector a only occur for a numeric value with more than one element.
Similarly,
paste( "hello", "there" )is equivalent to the string constant
'hello there'
By default, the string values are concatenated together using a single space. The optional sep= argument specifies a string to be used instead. For example,
paste("hello", "there", "how", "are", "you?", sep="XYZ")yields
helloXYZthereXYZhowXYZareXYZyou?
Note that the arguments to paste are first converted to scalar string's, and then concatenated together. So
paste( "hello there", 1:3, sep="" )yields
hello there[1 2 3]and not
hellothere[123]
If a single element string is passed to paste, the elements of the string are concatenated. For example:
paste("a b c d e", sep='' )results in the string "abcde". With multiple strings, the individual strings are concatenated, but not the individual elements of the strings.
spaste is simply a version of paste with the separator set to an empty string. It is defined using:
func spaste(...) paste(...,sep='')This form of paste is common enough that it merits its own simple form.
split is basically the inverse of paste. It takes a single argument, converts it to a scalar string, and splits it into words at each block of whitespace, just as string constants are constructed when enclosed in double-quotes (see § 3.3.1, page ). Thus
split('hello there how are you?')is equivalent to
"hello there how are you?"that is, it yields a five-element string vector.
You can also call split with a second argument. The second parameter specifies where in the string the break should occur, for example
split("hello there how are you", "eo")yields the equivalent of
['h', 'll', ' th', 'r', ' h', 'w ar', ' y', 'u']Here the first element is
'h'
, the second is 'll'
, the
third ' th'
, and so forth. The presence of the single leading space
in ' th'
may be surprising. What happened is that first split
converted
"hello there how are you"to a scalar value, equivalent to
'hello there how are you'since the double-quoted constant was constructed all information about the number of blanks between words was lost. Next split broke the scalar into words at every occurrence of an 'e' or an 'o', but not at each blank like it would without the second argument. If a blank had been included in the second argument then these extra blanks naturally disappear:
split("hello there how are you", 'eo ')yields the equivalent of
"h ll th r h w ar y u"Note that you have to enclose the second argument in single-quotes, otherwise the blank will be removed.
If the second argument is a null string, the first string is broken up into individual characters. Here is an example:
split("how are you", '')yields the string:
['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u']
The sprintf10.1 function is available for generating strings. This function takes the same arguments as the stdio.h sprintf function. This example:
sprintf("result: 0x%x", 2871)would return the string
'result: 0xb37'
. It uses the %x
field
specifier to output a hexadecimal number. %x
is just one of the
possible field specifiers available with sprintf (or printf or
fprintf); Table 10.1 lists all of the possible field
specifiers. Each of these field specifiers is prefixed with a percent sign and
indicates a field in the output string which is replaced with a subsequent
sprintf argument. Each field is replaced with the respective argument following
the format string. Here is another example:
sprintf("%4d/%02d/%02d %d:%02d:%02d",1998,8,20,7,46,2)In this case, a string like
'1998/08/20 7:46:02'
would be returned.
This example uses the %d
field specifier to create a date and time
string. %%
is used to put a percent sign into the result, like:
sprintf("%d%%", 100)which would create the string
'100%'
. In each case, the result of
sprintf is a string made up of the format string with each field
specifier replaced with a subsequent argument.
|
Each field specifier can have an extra qualifier between the %
and the
conversion character. The format of this qualifier is:
|
If sprintf is applied to vectors, the format string is applied to each element of the vectors. For example:
print sprintf('%d\t%r\n',1998:2002,1998:2002)would result in the following output:
1998 mcmxcviii 1999 mcmxcix 2000 mm 2001 mmi 2002 mmiiThe result of sprintf is a string, and the misalignment of 1998 and 1999 is the result of the space added to separate elements of a string. spaste, for example, could be used to get rid of this misalignment, or printf could be used instead of sprintf with print.
The tr function can be used to translate strings. It takes two ranges plus a string, and it maps characters of the first range occurring in a string to the corresponding character of the second range. Here are a couple of simple examples:
tr('[a-z]','[A-Z]',"This is a string.") tr('[a-z]','[Z-A]',"This is a string.") tr('[a-m][n-z][A-M][N-Z]','[n-z][a-m][N-Z][A-M]',"This is a string.")The result of these examples would be:
THIS IS A STRING. TSRH RH Z HGIRMT. Guvf vf n fgevat.The first example capitalizes all of the letters, the second jumbles them, and the third performs Rot13.
There are two other convienence functions based on tr, to_lower() and to_upper(). These functions convert a string to lower case or upper case.
strlen("the cow jumped over the moon")would return:
[3 3 6 4 3 4]