Common String Operations (GNU Octave (version 10.0.90))

#### 5.3.1 Common String Operations ¶

The following functions are useful to perform common String operations.

: `y =` lower `(s)` ¶
: `y =` tolower `(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:

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

  Programming Note: `tolower` is an alias for `lower` and either name can be used in Octave.

  See also: upper.

: `y =` upper `(s)` ¶
: `y =` toupper `(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:

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

  Programming Note: `toupper` is an alias for `upper` and either name can be used in Octave.

  See also: lower.

: `s =` deblank `(s)` ¶

  Remove trailing whitespace and nulls from s.

  If s is a matrix, deblank trims each row to the length of the 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.

: `s =` 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.

: `s =` 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.

: `str =` untabify `(t)` ¶
: `str =` untabify `(t, tw)` ¶
: `str =` 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.

: `newstr =` 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.

: `newstr =` undo_string_escapes `(string)` ¶

  Convert special characters in string 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.