[Next] [Prev] [_____] [Left] [Up] [Index] [Root]
Strings

Strings

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 line

Note 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.
Subsections

Creation of Strings

"abc" : -> MonStgElt
Create a string from a succession of keyboard characters (a, b, c) enclosed in double quotes " ".
s cat t : MonStgElt, MonStgElt -> MonStgElt
s * t : MonStgElt, MonStgElt -> MonStgElt
Concatenate the strings s and t.
s cat:= t : MonStgElt, MonStgElt -> MonStgElt
s *:= t : MonStgElt, MonStgElt -> MonStgElt
Modification-concatenation of the string s with t: concatenate s and t and put the result in s.
&cat s : [ MonStgElt ] -> MonStgElt
&* s : [ MonStgElt ] -> MonStgElt
Given an enumerated sequence s of strings, return the concatenation of these strings.
s ^ n : MonStgElt, RngIntElt -> MonStgElt
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.
s[i] : MonStgElt, RngIntElt -> MonStgElt
Return the substring of s consisting of the i-th character.

Integer-Valued Functions

# s : MonStgElt -> RngIntElt
The length of the string s.
Index(s, t) : MonStgElt, MonStgElt -> RngIntElt
Position(s, t) : MonStgElt, MonStgElt -> RngIntElt
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.)

Conversion Functions

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.
CodeToString(n) : RngIntElt -> MonStgElt
Returns a character (string of length 1) corresponding to the code number n, where the code is system dependent (see previous entry).
StringToInteger(s) : MonStgElt -> RngIntElt
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.
StringToIntegerSequence(s) : MonStgElt -> [ RngIntElt ]
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.
IntegerToString(n) : RngIntElt -> MonStgElt
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.)

Boolean Functions

s eq t : MonStgElt, MonStgElt -> BoolElt
Returns true if and only if the strings s and t are identical. Note that blanks are significant.
s ne t : MonStgElt, MonStgElt -> BoolElt
Returns true if and only if the strings s and t are distinct. Note that blanks are significant.
s in t : MonStgElt, MonStgElt -> BoolElt
Returns true if and only if s appears as a contiguous substring of t. Note that the empty string is contained in every string.
s notin t : MonStgElt, MonStgElt -> BoolElt
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.
s lt t : MonStgElt, MonStgElt -> BoolElt
True if s is lexicographically less than t, false otherwise. Here the ordering on characters imposed by their ASCII code number is used.
s le t : MonStgElt, MonStgElt -> BoolElt
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.
s gt t : MonStgElt, MonStgElt -> BoolElt
True if s is lexicographically greater than t, false otherwise. Here the ordering on characters imposed by their ASCII code number is used.
s ge t : MonStgElt, MonStgElt -> BoolElt
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.

Example Lang_Strings (H1E29)

> print "Mag" cat "ma";
Magma
Omitting double-quotes usually has undesired effects:

> print "Mag cat ma";
Mag cat ma
And 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;
true
The 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");
true
Finally, 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]