home *** CD-ROM | disk | FTP | other *** search
/ Math Solutions 1995 October / Math_Solutions_CD-ROM_Walnut_Creek_October_1995.iso / pc / mac / discrete / doc / string.tex < prev    next >
Encoding:
Text File  |  1993-05-05  |  12.7 KB  |  341 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%
  3. %A  string.tex                  GAP documentation            Martin Schoenert
  4. %%
  5. %A  @(#)$Id: string.tex,v 3.9 1993/02/19 10:48:42 gap Exp $
  6. %%
  7. %Y  Copyright 1990-1992,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
  8. %%
  9. %%  This file describes the string datatype and its functions.
  10. %%
  11. %H  $Log: string.tex,v $
  12. %H  Revision 3.9  1993/02/19  10:48:42  gap
  13. %H  adjustments in line length and spelling
  14. %H
  15. %H  Revision 3.8  1993/02/12  11:41:19  felsch
  16. %H  new example fixed
  17. %H
  18. %H  Revision 3.7  1993/02/11  12:20:47  martin
  19. %H  changed reference "Strings" to "Strings and Characters"
  20. %H
  21. %H  Revision 3.6  1993/02/11  12:10:05  martin
  22. %H  strings are now lists too
  23. %H
  24. %H  Revision 3.5  1993/02/09  10:40:18  felsch
  25. %H  examples fixed
  26. %H
  27. %H  Revision 3.4  1991/12/27  16:07:04  martin
  28. %H  revised everything for GAP 3.1 manual
  29. %H
  30. %H  Revision 3.3  1991/07/26  12:34:01  martin
  31. %H  improved the index
  32. %H
  33. %H  Revision 3.2  1991/07/26  09:01:07  martin
  34. %H  changed |\GAP\ | to |{\GAP}|
  35. %H
  36. %H  Revision 3.1  1991/07/25  16:16:59  martin
  37. %H  fixed some minor typos
  38. %H
  39. %H  Revision 3.0  1991/04/11  11:35:22  martin
  40. %H  Initial revision under RCS
  41. %H
  42. %%
  43. \Chapter{Strings and Characters}%
  44. \index{type!strings}%
  45. \index{doublequotes}\index{backslash}\index{special character sequences}
  46.  
  47. A *character*  is simply an object in {\GAP} that represents an arbitrary
  48. character from the character set  of  the  operating  system.   Character
  49. literals  can  be  entered  in  {\GAP}  by  enclosing  the  character  in
  50. *singlequotes* '\''.
  51.  
  52. |    gap> 'a';
  53.     'a'
  54.     gap> '*';
  55.     '*' |
  56.  
  57. A *string* is simply a dense list of characters.  Strings are used mainly
  58. in filenames and error messages.  A string literal can either be  entered
  59. simply  as  the  list of characters or by  writing the characters between
  60. *doublequotes* '\"'.   {\GAP}  will always output strings  in  the latter
  61. format.
  62.  
  63. |    gap> s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.'];
  64.     "Hallo world."
  65.     gap> s2 := "Hallo world.";
  66.     "Hallo world."
  67.     gap> s1 = s2;
  68.     true
  69.     gap> s3 := "";
  70.     ""    # the empty string
  71.     gap> s3 = [];
  72.     true |
  73.  
  74. Note that a string is just a special case  of a list.  So everything that
  75. is possible for lists (see  "Lists")  is also possible for strings.  Thus
  76. you can  access  the characters in  such  a string (see "List Elements"),
  77. test  for  membership (see  "In"), etc.  You can  even assign  to such  a
  78. string (see "List Assignment").  Of course unless you  assign a character
  79. in such a way  that the  list  stays dense,  the  resulting  list will no
  80. longer be a string.
  81.  
  82. |    gap> Length( s2 );
  83.     12
  84.     gap> s2[2];
  85.     'a'
  86.     gap> 'e' in s2;
  87.     false
  88.     gap> s2[2] := 'e';;  s2;
  89.     "Hello world." |
  90.  
  91. If a string is displayed as result of an evaluation (see "Main Loop"), it
  92. is  displayed  with enclosing   doublequotes.  However,  if  a  string is
  93. displayed by 'Print',  'PrintTo', or 'AppendTo' (see "Print",  "PrintTo",
  94. "AppendTo") the enclosing doublequotes are dropped.
  95.  
  96. |    gap> s2;
  97.     "Hello world."
  98.     gap> Print( s2 );
  99.     Hello world.gap> |
  100.  
  101. There  are a number  of *special character  sequences* that  can be  used
  102. between   the  single  quote  of  a  character  literal  or  between  the
  103. doublequotes  of  a  string  literal to  specify  characters,  which  may
  104. otherwise be inaccessible.  They consist of two characters.  The first is
  105. a  backslash $\backslash$.  The second may be any character.  The meaning
  106. is given in the following list
  107.  
  108. 'n':    *newline character*.  This  is  the character that, at   least on
  109.         UNIX systems, separates  lines in a  text file.  Printing of this
  110.         character in a string has the effect of moving  the  cursor  down
  111.         one line and back to the beginning  of the line.
  112.  
  113. '\"':    *doublequote character*.  Inside  a string a doublequote  must be
  114.         escaped by the backslash,  because it is otherwise interpreted as
  115.         end of the string.
  116.  
  117. '\'':    *singlequote character*.  Inside a character a  singlequote  must
  118.         escaped by the backslash, because it is  otherwise interpreted as
  119.         end of the character.
  120.  
  121. $\backslash$:  *backslash character*.  Inside  a string a backslash  must
  122.         be   escaped  by  another  backslash,    because it is  otherwise
  123.         interpreted as first character of an escape sequence.
  124.  
  125. 'b':    *backspace character*.  Printing this character should  have  the
  126.         effect of moving the cursor back one character.  Whether it works
  127.         or not is system dependent and should not be relied upon.
  128.  
  129. 'r':    *carriage return character*.  Printing this character should have
  130.         the effect of moving the cursor back to the beginning of the same
  131.         line.  Whether this works or not is again system dependent.
  132.  
  133. 'c':    *flush character*.  This character is not  printed.  Its  purpose
  134.         is to flush the output queue.  Usually {\GAP} waits until it sees
  135.         a <newline> before it prints a string.  If you want to display  a
  136.         string that does not include this character use $\backslash c$.
  137.  
  138. other:  For any other character the backslash is simply ignored.
  139.  
  140. Again, if the line is displayed as result of  an evaluation, those escape
  141. sequences are displayed in the  same way that they  are input.  They  are
  142. displayed in their special way only by 'Print', 'PrintTo', or 'AppendTo'.
  143.  
  144. |    gap> "This is one line.\nThis is another line.\n";
  145.     "This is one line.\nThis is another line.\n"
  146.     gap> Print( last );
  147.     This is one line.
  148.     This is another line. |
  149.  
  150. It is not allowed to enclose a <newline> inside the  string.  You can use
  151. the special  character  sequence  $\backslash n$  to  write  strings that
  152. include <newline>  characters.  If, however, a string  is too long to fit
  153. on a single line it is possible to *continue* it  over several lines.  In
  154. this case  the last character of  each  line, except the  last  must be a
  155. backslash.  Both backslash and <newline>  are thrown away.  Note that the
  156. same continuation mechanism is available for identifiers and integers.
  157.  
  158. |    gap> "This is a very long string that does not fit on a line \
  159.     gap> and is therefore continued on the next line.";
  160.     "This is a very long string that does not fit on a line and is therefo\
  161.     re continued on the next line."
  162.     # note that the output is also continued, but at a different place |
  163.  
  164. This chapter contains sections  describing  the function that creates the
  165. printable representation  of a string  (see "String"), the functions that
  166. create new   strings  (see    "ConcatenationString",  "SubString"),   the
  167. functions that tests  if an object  is   a  string  (see "IsString"), the
  168. string comparisons (see "Comparisons of Strings"), and the  function that
  169. returns the length of a string (see "LengthString").
  170.  
  171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  172. \Section{String}%
  173. \index{convert!to a string}
  174.  
  175. 'String( <obj> )' \\
  176. 'String( <obj>, <length> )'
  177.  
  178. 'String' returns a representation of the <obj>, which may be an object of
  179. arbitrary type, as a string.   This string should approximate  as closely
  180. as possible the character sequence you see if you print <obj>.
  181.  
  182. If <length> is given it must be an integer.  The absolute value gives the
  183. minimal  length of the  result.  If  the string  representation of  <obj>
  184. takes  less than that  many  characters  it is   filled with blanks.   If
  185. <length> is positive it is filled on the left, if <length> is negative it
  186. is filled on the right.
  187.  
  188. |    gap> String( 123 );
  189.     "123"
  190.     gap> String( [1,2,3] );
  191.     "[ 1, 2, 3 ]"
  192.     gap> String( 123, 10 );
  193.     "       123"
  194.     gap> String( 123, -10 );
  195.     "123       "
  196.     gap> String( 123, 2 );
  197.     "123" |
  198.  
  199. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200. \Section{ConcatenationString}%
  201. \index{concatenation!of strings}\index{append!one string to another}
  202.  
  203. 'ConcatenationString( <string1>, <string2> )'
  204.  
  205. 'ConcatenationString'   returns   the  concatenation of the   two strings
  206. <string1> and  <string2>.   This is a  new string  that  starts  with the
  207. characters of <string1> and ends with the characters of <string2>.
  208.  
  209. |    gap> ConcatenationString( "Hello ", "world.\n" );
  210.     "Hello world.\n" |
  211.  
  212. Because strings are now lists, 'Concatenation' (see "Concatenation") does
  213. exactly the  right  thing,  and  the  function  'ConcatenationString'  is
  214. obsolete.
  215.  
  216. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  217. \Section{SubString}%
  218. \index{substring!of a string}\index{part!of a string}
  219.  
  220. 'SubString( <string>, <from>, <to> )'
  221.  
  222. 'SubString' returns the substring  of the string  <string> that begins at
  223. position <from> and continues to  position <to>.  The characters at these
  224. two  positions are included.   Indexing is done with  origin 1, i.e., the
  225. first character is at position  1.  <from> and <to>  must be integers and
  226. are both silently forced into  the range '1..LengthString(<string>)' (see
  227. "LengthString").  If <to> is less than <from> the substring is empty.
  228.  
  229. |    gap> SubString( "Hello world.\n", 1, 5 );
  230.     "Hello"
  231.     gap> SubString( "Hello world.\n", 5, 1 );
  232.     "" |
  233.  
  234. Because strings are  now  lists, substrings  can  also be extracted  with
  235. '<string>\{[<from>..<to>]\}' (see "List  Elements").  'SubString'  forces
  236. <from> and <to> into  the range  '1..Length(<string>)', which  the  above
  237. does not, but apart from that 'SubString' is obsolete.
  238.  
  239. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  240. \Section{Comparisons of Strings}%
  241. \index{comparisons!of strings}
  242.  
  243. '<string1> = <string2>', '<string1> \<> <string2>'
  244.  
  245. The  equality  operator  '='   evaluates to  'true'   if  the two strings
  246. <string1> and <string2> are equal and  'false' otherwise.  The inequality
  247. operator '\<>' returns 'true' if  the two strings <string1> and <string2>
  248. are not equal and 'false' otherwise.
  249.  
  250. |    gap> "Hello world.\n" = "Hello world.\n";
  251.     true
  252.     gap> "Hello World.\n" = "Hello world.\n";
  253.     false    # string comparison is case sensitive
  254.     gap> "Hello world." = "Hello world.\n";
  255.     false    # the first string has no <newline>
  256.     gap> "Goodbye world.\n" = "Hello world.\n";
  257.     false |
  258.  
  259. '<string1> \<\ <string2>', '<string1> \<= <string2>',
  260. '<string1>  > <string2>', '<string1>  => <string2>'
  261.  
  262. The operators '\<', '\<=', '>', and '=>' evaluate to 'true' if the string
  263. <string1> is less than, less than or equal to, greater than, greater than
  264. or  equal to the string  <string2> respectively.  The ordering of strings
  265. is  lexicographically according  to the order  implied by the underlying,
  266. system dependent, character set.
  267.  
  268. You  can   compare  strings  with objects   of   other types.   Integers,
  269. rationals, cyclotomics,  permutations,  words,  and  boolean  values  are
  270. smaller  than strings.  Objects   of  the other types, i.e.,   functions,
  271. lists, and records are larger.
  272.  
  273. |    gap> "Hello world.\n" < "Hello world.\n";
  274.     false    # the strings are equal
  275.     gap> "Hello World.\n" < "Hello world.\n";
  276.     true    # in ASCII uppercase letters come before lowercase letters
  277.     gap> "Hello world." < "Hello world.\n";
  278.     true    # prefixes are always smaller
  279.     gap> "Goodbye world.\n" < "Hello world.\n";
  280.     true    # 'G' comes before 'H', in ASCII at least |
  281.  
  282. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  283. \Section{IsString}%
  284. \index{test!for a string}
  285.  
  286. 'IsString( <obj> )'
  287.  
  288. 'IsString' returns 'true' if the object <obj>, which  may be an object of
  289. arbitrary type, is a  string and 'false' otherwise.   Will cause an error
  290. if <obj> is an unbound variable.
  291.  
  292. |    gap> IsString( "Hello world.\n" );
  293.     true
  294.     gap> IsString( "123" );
  295.     true
  296.     gap> IsString( 123 );
  297.     false
  298.     gap> IsString( [ '1', '2', '3' ] );
  299.     true
  300.     gap> IsString( [ '1', '2', , '4' ] );
  301.     false    # strings must be dense
  302.     gap> IsString( [ '1', '2', 3 ] );
  303.     false    # strings must only contain characters |
  304.  
  305. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  306. \Section{LengthString}%
  307. \index{length!of a string}
  308.  
  309. 'LengthString( <string> )'
  310.  
  311. 'LengthString' returns the  length of the string <string>.  The length of
  312. a  string is the number of  characters in  the string.   Escape sequences
  313. (see "Strings  and  Characters")  are just a two character representation
  314. for a single character,  and  are thus  counted  as  single  character by
  315. 'LengthString'.
  316.  
  317. |    gap> LengthString( "" );
  318.     0
  319.     gap> LengthString( "Hello" );
  320.     5
  321.     gap> LengthString( "Hello world.\n" );
  322.     13 |
  323.  
  324. Because strings are now lists, 'Length' (see  "Length") does exactly  the
  325. right thing, and the function 'LengthString' is obsolete.
  326.  
  327. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  328. %%
  329. %E  Emacs . . . . . . . . . . . . . . . . . . . . . local Emacs variables
  330. %%
  331. %%  Local Variables:
  332. %%  mode:               outline
  333. %%  outline-regexp:     "\\\\Chapter\\|\\\\Section"
  334. %%  fill-column:        73
  335. %%  eval:               (hide-body)
  336. %%  End:
  337. %%
  338.  
  339.  
  340.  
  341.