Splitting and Joining Strings (GNU Octave (version 10.0.90))

#### 5.3.3 Splitting and Joining Strings ¶

: `str =` substr `(s, offset)` ¶
: substr `(s, offset, len)` ¶

  Return the substring of s which starts at character number offset and is len characters long.

  Position numbering for offsets begins with 1. If offset is negative, extraction starts that far from the end of the string.

  If len is omitted, the substring extends to the end of s. A negative value for len extracts to within len characters of the end of the string

  Examples:

      substr ("This is a test string", 6, 9)
           ⇒ "is a test"
      substr ("This is a test string", -11)
           ⇒ "test string"
      substr ("This is a test string", -11, -7)
           ⇒ "test"

  This function is patterned after the equivalent function in Perl.

: `[tok, rem] =` strtok `(str)` ¶
: `[tok, rem] =` strtok `(str, delim)` ¶

  Find all characters in the string str up to, but not including, the first character which is in the string delim.

  str may also be a cell array of strings in which case the function executes on every individual string and returns a cell array of tokens and remainders.

  Leading delimiters are ignored. If delim is not specified, whitespace is assumed.

  If rem is requested, it contains the remainder of the string, starting at the first delimiter.

  Examples:

      strtok ("this is the life")
           ⇒ "this"

      [tok, rem] = strtok ("14*27+31", "+-*/")
           ⇒
              tok = 14
              rem = *27+31

  See also: index, strsplit, strchr, isspace.

: `[cstr] =` strsplit `(str)` ¶
: `[cstr] =` strsplit `(str, del)` ¶
: `[cstr] =` strsplit `(…, name, value)` ¶
: `[cstr, matches] =` strsplit `(…)` ¶

  Split the string str using the delimiters specified by del and return a cell string array of substrings.

  If a delimiter is not specified the string is split at whitespace `{" ", "\f", "\n", "\r", "\t", "\v"}`. Otherwise, the delimiter, del must be a string or cell array of strings. By default, consecutive delimiters in the input string s are collapsed into one resulting in a single split.

  Supported name/value pair arguments are:

  - collapsedelimiters which may take the value of `true` (default) or `false`.
  - delimitertype which may take the value of `"simple"` (default) or `"regularexpression"`. A simple delimiter matches the text exactly as written. Otherwise, the syntax for regular expressions outlined in `regexp` is used.

  The optional second output, matches, returns the delimiters which were matched in the original string.

  Examples with simple delimiters:

      strsplit ("a b c")
            ⇒
                {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = c
                }

      strsplit ("a,b,c", ",")
            ⇒
                {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = c
                }

      strsplit ("a foo b,bar c", {" ", ",", "foo", "bar"})
            ⇒
                {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = c
                }

      strsplit ("a,,b, c", {",", " "}, "collapsedelimiters", false)
            ⇒
                {
                  [1,1] = a
                  [1,2] =
                  [1,3] = b
                  [1,4] =
                  [1,5] = c
                }

  Examples with regularexpression delimiters:

      strsplit ("a foo b,bar c", ',|\s|foo|bar', ...
                "delimitertype", "regularexpression")
      ⇒
      {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = c
      }

      strsplit ("a,,b, c", '[, ]', "collapsedelimiters", false, ...
                "delimitertype", "regularexpression")
      ⇒
      {
                  [1,1] = a
                  [1,2] =
                  [1,3] = b
                  [1,4] =
                  [1,5] = c
      }

      strsplit ("a,\t,b, c", {',', '\s'}, "delimitertype", "regularexpression")
      ⇒
      {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = c
      }

      strsplit ("a,\t,b, c", {',', ' ', '\t'}, "collapsedelimiters", false)
      ⇒
      {
                  [1,1] = a
                  [1,2] =
                  [1,3] =
                  [1,4] = b
                  [1,5] =
                  [1,6] = c
      }

  See also: ostrsplit, strjoin, strtok, regexp.

: `[cstr] =` ostrsplit `(s, sep)` ¶
: `[cstr] =` ostrsplit `(s, sep, strip_empty)` ¶

  Split the string s using one or more separators sep and return a cell array of strings.

  Consecutive separators and separators at boundaries result in empty strings, unless strip_empty is true. The default value of strip_empty is false.

  2-D character arrays are split at separators and at the original column boundaries.

  Example:

      ostrsplit ("a,b,c", ",")
            ⇒
                {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = c
                }

      ostrsplit (["a,b" ; "cde"], ",")
            ⇒
                {
                  [1,1] = a
                  [1,2] = b
                  [1,3] = cde
                }

  See also: strsplit, strtok.

: `str =` strjoin `(cstr)` ¶
: `str =` strjoin `(cstr, delimiter)` ¶

  Join the elements of the cell string array, cstr, into a single string.

  If no delimiter is specified, the elements of cstr are separated by a space.

  If delimiter is specified as a string, the cell string array is joined using the string. Escape sequences are supported.

  If delimiter is a cell string array whose length is one less than cstr, then the elements of cstr are joined by interleaving the cell string elements of delimiter. Escape sequences are not supported.

      strjoin ({'Octave','Scilab','Lush','Yorick'}, '*')
            ⇒ 'Octave*Scilab*Lush*Yorick'

  See also: strsplit.