Navigation

Operators and Keywords

Function List:

C++ API

Function File: [cstr] = strsplit (s)

Function File: [cstr] = strsplit (s, del)

Function File: [cstr] = strsplit (..., name, value)

Function File: [cstr, matches] = strsplit (...)

Split the string s using the delimiters specified by del and return a cell-string array of sub-strings. If a delimiter is not specified the string, s, is split at whitespace. The delimiter, del may be a string, a scalar cell-string, or cell-string array. del must be a cell-string array. By default, consecutive delimiters in the input string, s, are collapsed into one.

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

Example:

           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", {"\s", "foo", "bar"})
                 ⇒
                     {
                       [1,1] = a
                       [1,2] = b
                       [1,3] = c
                     }
          
           strsplit ("a,,b, c", {",", " "}, false)
                 ⇒
                     {
                       [1,1] = a
                       [1,2] =
                       [1,3] = b
                       [1,4] =
                       [1,5] = c
                     }
          

Supported name/value pair arguments are;

  • collapsedelimiters may take the value of true or false with the default being false.
  • delimitertype may take the value of simple or regularexpression. The default is delimitertype is simple.

Example:

           strsplit ("a foo b,bar c", ",|\\s|foo|bar", "delimitertype", "regularexpression")
                 ⇒
                     {
                       [1,1] = a
                       [1,2] = b
                       [1,3] = c
                     }
          
           strsplit ("a,,b, c", "[, ]", 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