Navigation

Operators and Keywords

Function List:

C++ API

: [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.

Package: octave