Next Previous Contents

13. String commands

In addition to being able to manipulate real numbers, calc is also capable of dealing with strings. To include a string in the stack, enter the string between double quotes, for example:

> "This is a string"
[ 1]        "This is a string"
>

There is no limit on the size of a string, it is limited only by the available memory. I've been able to successfully put a 15-megabyte string in the stack. However, I'd not recommend trying to do this unless you have a lot of memory. And don't try this on MS-DOS either.

The commands which deal with strings are:

13.1 The + command (for strings)

The + command, when at least one argument is a string, does string concatenation. The string at level one is appended at the end of the string at level two. If one of the two arguments is not a string, it will be converted to a string.

Examples:

> "Hello, " "World!"
[ 2]                 "Hello, "
[ 1]                  "World!"
> +
[ 1]           "Hello, World!"
> 45 +
[ 1]         "Hello, World!45"

13.2 The size command

This command returns a real number representing the size of the string given as argument, in bytes.

Examples:

> "Hello, World!"
[ 1]           "Hello, World!"
> size
[ 1]                        13

13.3 The >str command

This command converts its argument to a string representing it. Numbers are converted using the current setting of precision, number format and coordinate mode (see section Customization). Strings are not modified.

Examples:

> (1, 3)
[ 1]                     (1,3)
> >str
[ 1]                   "(1,3)"
> 4 >str
[ 2]                   "(1,3)"
[ 1]                       "4"

13.4 The str> command

This command takes a string as argument, and evaluates it as if it were a series of commands typed at command line.

Example:

> "6 16 sqrt +"
[ 1]             "6 16 sqrt +"
> str>
[ 1]                        10

13.5 The num command

This command takes a string and returns a real number, whose value is the ASCII code for the first character of the string.

Examples:

> "A"
[ 1]                       "A"
> num
[ 1]                        65
> "c" num
[ 2]                        65
[ 1]                        99

13.6 The chr command

This command takes a real number representing an ASCII code as argument, and returns a string with the character that code represents.

Examples:

> 65 chr
[ 1]                       "A"
> 99 chr
[ 2]                       "A"
[ 1]                       "c"

13.7 The head command

This command takes a string and returns another string containing just the first character of the string.

Examples:

> "Hello, World"
[ 1]            "Hello, World"
> head
[ 1]                       "H"

13.8 The tail command

This command takes a string and returns another string containing the input string minus its first character.

Examples:

> "Hello, World"
[ 1]            "Hello, World"
> tail
[ 1]             "ello, World"

13.9 The pos command

This command takes two strings as argument. The string in level one is searched in the string in level two. A real number is returned, representing the position of the start of the substring in the string. If no match was found, zero is returned.

Examples:

> "Hello, World" "W" pos
[ 1]                         8
> "Hello, World" "x" pos
[ 2]                         8
[ 1]                         0

13.10 The sub command

This command takes three arguments: a string in level three, and two real numbers in levels two and one, representing the starting position and the ending position. A new string is returned, which is a the part of the input string between the starting and ending positions, inclusive.

If the starting position is less than one, it is considered as one. If the ending position is greater than the length of the string, it is considered as the length of the string. If the ending position is less than the starting position, an empty string is returned.

Examples:

> "Hello, World"
[ 1]            "Hello, World"
> 4 8 sub
[ 1]                   "lo, W"
> 3 3 sub
[ 1]                       ","

13.11 The repl command

This command takes three arguments: a string in level three, a real number in level two and another string in level one. The characters of the string in level three, starting at the position in level two, are substituted by the string in level one.

Examples:

> "Hello John" 7 "Mary" repl
[ 1]              "Hello Mary"
> 7 "L" repl
[ 1]              "Hello Lary"
> 10 "ry" repl
[ 1]             "Hello Larry"

13.12 The str>id and $>id commands

These command convert the string in level one to an identifier.

Examples:

> "Hello"
[ 1]                   "Hello"
> str>id
[ 1]                   'Hello'
> "World" $>id
[ 2]                   'Hello'
[ 1]                   'World'

13.13 The id>str and id>$ commands

These commands convert the identifier in level one to a string. The difference between these commands and >str is that the latter inserts the quotes as part of the string.

Examples:

> 'Hello'
[ 1]                   'Hello'
> id>str
[ 1]                   "Hello"
> 'World' id>$
[ 2]                   "Hello"
[ 1]                   "World"
> 'World' >str
[ 3]                   "Hello"
[ 2]                   "World"
[ 1]                 "'World'"


Next Previous Contents