26 Strings and Characters

  • IsChar( obj ) C
  • IsCharCollection( obj ) C

    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> x:= 'a';  IsChar( x );
    'a'
    true
    gap> '*';
    '*'
    

  • IsString( obj ) C

    A string is simply a dense list (see IsList, IsDenseList) of characters (see IsChar); thus strings are always homogeneous (see IsHomogeneousList). 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','e','l','l','o',' ','w','o','r','l','d','.'];
    "Hello world."
    gap> IsString( s1 );
    true
    gap> s2 := "Hello world.";
    "Hello world."
    gap> s1 = s2;
    true
    gap> s3 := "";
    ""           # the empty string
    gap> s3 = [];
    true
    gap> IsString( [] );
    true
    gap> IsString( "123" );  IsString( 123 );
    true
    false
    gap> IsString( [ '1', '2', '3' ] );
    true
    gap> IsString( [ '1', '2', , '4' ] );  IsString( [ '1', '2', 3 ] );
    false        # strings must be dense
    false        # strings must only contain characters
    

    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 Membership Test for Collections), ask for the length, concatenate strings (see Concatenation), form substrings etc. You can even assign to a mutable 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];
    'e'
    gap> 'a' in s2;
    false
    gap> s2[2] := 'a';;  s2;
    "Hallo world."
    gap> s1{ [1..4] };
    "Hell"
    gap> Concatenation( s1{ [ 1 .. 6 ] }, s1{ [ 1 .. 4 ] } );
    "Hello Hell"
    

    If a string is displayed by View, for example as result of an evaluation (see Main Loop), or by ViewObj and PrintObj, it is displayed with enclosing doublequotes. However, if a string is displayed by Print, PrintTo, or AppendTo (see View and Print, PrintTo, AppendTo) the enclosing doublequotes are dropped. So strings behave differently from other GAP objects w.r.t. printing in the sense that the output of Print for a string is not equal to the output of PrintObj.

    gap> s4:= "abc\"def\nghi";;
    gap> View( s4 );  Print( "\n" );
    "abc\"def\nghi"
    gap> ViewObj( s4 );  Print( "\n" );
    "abc\"def\nghi"
    gap> PrintObj( s4 );  Print( "\n" );
    "abc"def\nghi"
    gap> Print( s4 );  Print( "\n" );
    abc"def
    ghi
    

    Note that only those line breaks are printed by Print that are contained in the string (\n characters, see Special Characters), as is shown in the example below.

    gap> s1;
    "Hello world."
    gap> Print( s1 );
    Hello world.gap> Print( s1, "\nnext line\n" );
    Hello world.
    next line
    gap>
    

    Sections

    1. Special Characters
    2. Recognizing Characters
    3. Comparisons of Strings
    4. Operations to Produce Strings
    5. Operations to Evaluate Strings
    6. Calendar Arithmetic
    7. Internally Represented Strings

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000