A character is simply an object in GAP that represents an arbitrary
character from the character set of the operating system. Character
literals can be entered in GAP by enclosing the character in
singlequotes '
.
gap> 'a'; 'a' gap> '*'; '*'
A string is simply a dense list of characters. Strings are used mainly
in filenames and error messages. A string literal can either be entered
simply as the list of characters or by writing the characters between
doublequotes "
. GAP will always output strings in the latter
format.
gap> s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.']; "Hallo world." gap> s2 := "Hallo world."; "Hallo world." gap> s1 = s2; true gap> s3 := ""; "" # the empty string gap> s3 = []; true
Note that a string is just a special case of a list. So everything that is possible for lists (see Lists) is also possible for strings. Thus you can access the characters in such a string (see List Elements), test for membership (see In), etc. You can even assign to such a string (see List Assignment). Of course unless you assign a character in such a way that the list stays dense, the resulting list will no longer be a string.
gap> Length( s2 ); 12 gap> s2[2]; 'a' gap> 'e' in s2; false gap> s2[2] := 'e';; s2; "Hello world."
If a string is displayed as result of an evaluation (see Main Loop), it
is displayed with enclosing doublequotes. However, if a string is
displayed by Print
, PrintTo
, or AppendTo
(see Print, PrintTo,
AppendTo) the enclosing doublequotes are dropped.
gap> s2; "Hello world." gap> Print( s2 ); Hello world.gap>
There are a number of special character sequences that can be used between the single quote of a character literal or between the doublequotes of a string literal to specify characters, which may otherwise be inaccessible. They consist of two characters. The first is a backslash \ . The second may be any character. The meaning is given in the following list
n
:
"
:
'
:
b
:
r
:
c
:
Again, if the line is displayed as result of an evaluation, those escape
sequences are displayed in the same way that they are input. They are
displayed in their special way only by Print
, PrintTo
, or AppendTo
.
gap> "This is one line.\nThis is another line.\n"; "This is one line.\nThis is another line.\n" gap> Print( last ); This is one line. This is another line.
It is not allowed to enclose a newline inside the string. You can use the special character sequence \ n to write strings that include newline characters. If, however, a string is too long to fit on a single line it is possible to continue it over several lines. In this case the last character of each line, except the last must be a backslash. Both backslash and newline are thrown away. Note that the same continuation mechanism is available for identifiers and integers.
gap> "This is a very long string that does not fit on a line \ gap> and is therefore continued on the next line."; "This is a very long string that does not fit on a line and is therefo\ re continued on the next line." # note that the output is also continued, but at a different place
This chapter contains sections describing the function that creates the printable representation of a string (see String), the functions that create new strings (see ConcatenationString, SubString), the functions that tests if an object is a string (see IsString), the string comparisons (see Comparisons of Strings), and the function that returns the length of a string (see LengthString).
GAP 3.4.4