Next: Concatenating Strings, Up: String Operations [Contents][Index]

#### 5.3.1 Common String Operations

The following functions are useful to perform common String operations.

: tolower (s) ¶
: lower (s) ¶

  Return a copy of the string or cell string s, with each uppercase character replaced by the corresponding lowercase one; non-alphabetic characters are left unchanged.

  For example:

      tolower ("MiXeD cAsE 123")
            ⇒ "mixed case 123"

  See also: toupper.

: toupper (s) ¶
: upper (s) ¶

  Return a copy of the string or cell string s, with each lowercase character replaced by the corresponding uppercase one; non-alphabetic characters are left unchanged.

  For example:

      toupper ("MiXeD cAsE 123")
            ⇒ "MIXED CASE 123"

  See also: tolower.

: deblank (s) ¶

  Remove trailing whitespace and nulls from s.

  If s is a matrix, deblank trims each row to the length of longest string. If s is a cell array of strings, operate recursively on each string element.

  Examples:

      deblank ("    abc  ")
           ⇒  "    abc"

      deblank ([" abc   "; "   def   "])
           ⇒  [" abc  " ; "   def"]

  See also: strtrim.

: strtrim (s) ¶

  Remove leading and trailing whitespace from s.

  If s is a matrix, strtrim trims each row to the length of longest string. If s is a cell array of strings, operate recursively on each string element.

  For example:

      strtrim ("    abc  ")
           ⇒  "abc"

      strtrim ([" abc   "; "   def   "])
           ⇒  ["abc  "  ; "  def"]

  See also: deblank.

: strtrunc (s, n) ¶

  Truncate the character string s to length n.

  If s is a character matrix, then the number of columns is adjusted.

  If s is a cell array of strings, then the operation is performed on each cell element and the new cell array is returned.

: untabify (t) ¶
: untabify (t, tw) ¶
: untabify (t, tw, deblank) ¶

  Replace TAB characters in t with spaces.

  The input, t, may be either a 2-D character array, or a cell array of character strings. The output is the same class as the input.

  The tab width is specified by tw, and defaults to eight.

  If the optional argument deblank is true, then the spaces will be removed from the end of the character data.

  The following example reads a file and writes an untabified version of the same file with trailing spaces stripped.

      fid = fopen ("tabbed_script.m");
      text = char (fread (fid, "uchar")');
      fclose (fid);
      fid = fopen ("untabified_script.m", "w");
      text = untabify (strsplit (text, "\n"), 8, true);
      fprintf (fid, "%s\n", text{:});
      fclose (fid);

  See also: strjust, strsplit, deblank.

: do_string_escapes (string) ¶

  Convert escape sequences in string to the characters they represent.

  Escape sequences begin with a leading backslash (`'\'`) followed by 1–3 characters (.e.g., `"\n"` => newline).

  See also: undo_string_escapes.

: undo_string_escapes (s) ¶

  Convert special characters in strings back to their escaped forms.

  For example, the expression

      bell = "\a";

  assigns the value of the alert character (control-g, ASCII code 7) to the string variable `bell`. If this string is printed, the system will ring the terminal bell (if it is possible). This is normally the desired outcome. However, sometimes it is useful to be able to print the original representation of the string, with the special characters replaced by their escape sequences. For example,

      octave:13> undo_string_escapes (bell)
      ans = \a

  replaces the unprintable alert character with its printable representation.

  See also: do_string_escapes.

Next: Concatenating Strings, Up: String Operations [Contents][Index]