String



next up previous contents index
Next: Array Up: No Title Previous: Char

String

A "string" is an immutable sequence of character. It is indexable; the low bound of a "string" is 1. The size of strings is limited to what can be indexed using ints. Thus the largest string has high bound (and size) "int_max".

A "string" literal is written as a sequence of zero or more character representations enclosed in double quotes. Within a string literal, a printing ASCII character other than double quote or backslash is represented by itself. Any character can be represented by using the escape sequences listed for characters. Examples of string literals are

"", "Item\tCost", "hello\n", "\"string\"", "'c'"

Methods for type "string"

  length ( ) returns (int)
      % effects   returns the size of self.

  empty ( ) returns (bool)
      % effects   returns (self.length() = 0)

  fetch (i: int) returns (char) signals (bounds)
      % effects   if i is within bounds returns the ith character of self else signals bounds.

  rest (i: int) returns (string) signals (bounds)
      % effects   returns a string containing self[i],...,self[self.length()];
      %           signals bounds if i is not a legal index in self.

  first (i: int) returns (string) signals (bounds)
      % effects   returns a string containing self[1], ..., self[i];
      %           signals bounds if i is not a legal index in self.

  concat (s: string) returns (string) 
      % effects   returns a string containing the characters of self followed by the
      %           characters of s; signals failure if the resulting string is bigger
      %           than what can be represented

  append (c: char) returns (string) 
      % effects   returns a string containing the characters of self followed by
      %           c;  signals failure if the resulting string is bigger than what
      %           can be represented.  This method has the same effect as
      %                         self.concat(c.to_string())

  extract (at: int, count: int) returns (string) signals (bounds, negative_size)
      % effects   If count is negative, signals negative_size.
      %           If at isn't a legal index in self, signals bounds.
      %           Otherwise returns a new string containing the characters
      %           self[at], self[at+1], ...; the new string contains
      %           min(count, self.length() - at + 1) characters.  For example, if s = "abcdef", then
      %                 s.substr(2, 3)   = "bcd"
      %                 s.substr(2, 7)   = "bcdef"

  chars ( ) yields (char)
      % effects   yields the characters of self in order from the first to last.

  lt (s: string) returns (bool)
  le (s: string) returns (bool)
  ge (s: string) returns (bool)
  gt (s: string) returns (bool)
      % effects   these are the usual lexicographic ordering operations based on the
      %           ASCII ordering of the characters contained in self and s.

  equal (s: string) returns (bool)
      % effects   returns (self = s)

  similar (s: string) returns (bool)
      % effects   returns (self = s)

  copy ( ) returns (string)
      % effects   returns a string that contains self[1],...,self[self.size()]

  unparse ( ) returns (string)
      % effects   Returns the concatenation of the strings produced by calling unparse
      %           on all of the characters contained in self.
Routines for type "string"
  string_create (chars: sequence[char]) returns (string)
      % effects   returns a new string containing the
      %           characters in the sequence in order.



theta-questions@lcs.mit.edu