Strings

A string is an immutable sequence of characters that may be used for text meant to be printed to the screen; labels, legends, or titles in plots; or stored in a file.

[> str1 := "Hello world!":

Like C++, the backslash character can be used to escape, for example a double quote character, and other non-printing characters.

[> str2 := "Hello, \"world\"!\nHow are you?":

Also like C++, consecutive strings separated only be white-space (be it spaces, tabs or line breaks) are automatically concatenated into a single string.

[> str3 := "123" "456"
                  "789";

$str3 := "123456789"$

You can access a sub-string by using the indexing operator, but like everything else in Maple, the first character is associated with the index 1:

[> str3[4..-2];

$"45678"$

Maple does not have a character data type, so extracting a single character returns a string of length 1:

[> str3[-2];

$"7"$

To calculate the length of a string, use the length(...) command:

[> length( str2 );

$28$

You'll notice that the escaped characters, while requiring two characters to define, require only one character in storage, as expected.

The StringTools package allows you to efficiently manipulate strings in Maple, and there is a StringTools[PatternDictionary] sub-package.