Strings of characters only have a limited use in Magma --- primarily in output formatting and specification of optional parameters to intrinsic functions --- hence only a limited number of operations are defined on them. However, if one wishes, a more extensive set of operations may be performed by converting a string into a sequence, applying the operation on the sequence and then converting back to a string. We will give some examples of this below.
Strings may consist of all ordinary characters appearing on your keyboard, including the blank (space). Two symbols have a special meaning: the double-quote " and the backslash . The double-quote is used to delimit a character string, and hence cannot be used inside a string; to be able to use a double-quote in strings the backslash is designed to be an escape character and is used to indicate that the next symbol has to be taken literally; thus, by using \" inside a string one indicates that the symbol " has to be taken literally and is not to be interpreted as the end-of-string delimiter. Thus:
> print "\"Print this line in quotes\""; "Print this line in quotes"To obtain a literal backslash, one simply types two backslashes; for characters other than double-quotes and backslash it does not make a difference when a backslash precedes them inside a string, with the exception of n, r and t. Any occurrence of \n or \r inside a string is converted into a <new-line> while \t is converted into a <tab>. For example:
> print "The first line,\nthe second line, and then\ran\tindented line"; The first line, the second line, and then an indented lineNote that a backslash followed by a return allows one to conveniently continue the current construction on the next line; so \<return> inside a string will be ignored, except that input will continue on a new line on your screen.
Create a string from a succession of keyboard characters (a, b, c) enclosed in double quotes " ".
Concatenate the strings s and t.
Modification-concatenation of the string s with t: concatenate s and t and put the result in s.
Given an enumerated sequence s of strings, return the concatenation of these strings.
Form the n-fold concatenation of the string s, for n >= 0. If n=0 this is the empty string, if n=1 it equals s, etc.
Return the substring of s consisting of the i-th character.
The length of the string s.
This function returns the position (an integer p with 0 < p <= #s) in the string s where the beginning of a contiguous substring t occurs. It returns 0 if t is not a substring of s. (If t is the empty string, position 1 will always be returned, even if s is empty as well.)
To perform more sophisticated operations, one may convert
the string into a sequence and use the extensive facilities for
sequences described in the next part of this manual; see the examples
at the end of this chapter for details.
StringToCode(s) : MonStgElt -> RngIntElt
Returns the code number of the first character of string s. This code depends on the computer system that is used; it is ASCII on most UNIX machines.
Returns a character (string of length 1) corresponding to the code number n, where the code is system dependent (see previous entry).
Returns the integer corresponding to the string of decimal digits s. All non-space characters in the string s must be digits (0, 1, ..., 9), except the first character, which is also allowed to be + or -. An error results if any other combination of characters occurs. Leading zeros are omitted.
Returns the sequence of integers corresponding to the string s of space-separated decimal numbers. All non-space characters in the string s must be digits (0, 1, ..., 9), except the first character after each space, which is also allowed to be + or -. An error results if any other combination of characters occurs. Leading zeros are omitted. Each number can begin with a sign (+ or -) without a space.
Convert the integer n into a string of digits; if n is negative the first character of the string will be -. (Note that leading zeros and a + sign are ignored when Magma builds an integer, so the resulting string will never begin with + or 0 characters.)
Returns true if and only if the strings s and t are identical. Note that blanks are significant.
Returns true if and only if the strings s and t are distinct. Note that blanks are significant.
Returns true if and only if s appears as a contiguous substring of t. Note that the empty string is contained in every string.
Returns true if and only if s does not appear as a contiguous substring of t. Note that the empty string is contained in every string.
True if s is lexicographically less than t, false otherwise. Here the ordering on characters imposed by their ASCII code number is used.
True if s is lexicographically less than or equal to t, false otherwise. Here the ordering on characters imposed by their ASCII code number is used.
True if s is lexicographically greater than t, false otherwise. Here the ordering on characters imposed by their ASCII code number is used.
True if s is lexicographically greater than or equal to t, false otherwise. Here the ordering on characters imposed by their ASCII code number is used.
> print "Mag" cat "ma"; MagmaOmitting double-quotes usually has undesired effects:
> print "Mag cat ma"; Mag cat maAnd note that there are two different equalities involved in the following!
> print "73" * "9" * "42" eq "7" * "3942"; true > print 73 * 9 * 42 eq 7 * 3942; trueThe next line shows how strings can be concatenated quickly, and also that strings of blanks can be used for formatting:
> s := ("Mag" cat "ma? ")^2;
> print s, " "^30, s[4]^12, "!";
Magma? Magma? mmmmmmmmmmmm !
Here is a way to list (in a sequence) the first occurrence
of each of the ten digits in the decimal expansion of pi, using
IntegerToString and Position.
> pi := Pi(RealField(1001)); > dec1000 := Round(10^1000*(pi-3)); > I := IntegerToString(dec1000); > print [ Position(I, IntegerToString(i)) : i in [0..9] ]; [ 32, 1, 6, 9, 2, 4, 7, 13, 11, 5 ]Using the length # and string indexing [ ] it is also easy to count the number of occurrences of each digit in the string containing the first 1000 digits.
> print [ #[i : i in [1..#I] | I[i] eq IntegerToString(j)] : j in [0..9] ]; [ 93, 116, 103, 102, 93, 97, 94, 95, 101, 106 ]We would like to test if the ASCII-encoding of the string `Magma' appears. This could be done as follows, using StringToCode and in, or alternatively, Position. To reduce the typing, we first abbreviate IntegerToString to is and StringToCode to sc.
> sc := StringToCode;
> its := IntegerToString;
> M := its(sc("M")) * its(sc("a")) * its(sc("g")) * its(sc("m")) * its(sc("a"));
> print M;
779710310997
> print M in I;
false
> print Position(I, M);
0
So `Magma' does not appear this way. However, we could be satisfied if the letters
appear somewhere in the right order. To do more sophisticated operations (like this)
on strings, it is necessary to convert the string into a sequence, because
sequences constitute a more versatile data type, allowing many more advanced
operations than strings.
> Iseq := [ I[i] : i in [1..#I] ]; > Mseq := [ M[i] : i in [1..#M] ]; > print IsSubsequence(Mseq, Iseq); false > print IsSubsequence(Mseq, Iseq: Kind := "Sequential"); trueFinally, we find that the string `magma' lies in between `Pi' and `pi':
> print "Pi" le "magma"; true > print "magma" lt "pi"; true[Next] [Prev] [_____] [Left] [Up] [Index] [Root]