Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

split

Split strings in string array

Syntax

newStr = split(str)
newStr = split(str,delimiter)
newStr = split(str,delimiter,dim)
[newStr,matches] = split(___)

Description

example

newStr = split(str) divides str at whitespace characters and returns the result as the output array newStr. The input array str can be a string array, a character vector, or a cell array of character vectors. If str is a string array, then so is newStr. Otherwise, newStr is a cell array of character vectors. newStr does not include the whitespace characters from str.

If str contains multiple elements, then each element must be divisible into the same number of substrings.

  • If str is a string scalar or character vector, then newStr is an N-by-1 string array or cell array of character vectors, where N is the number of substrings.

  • If str is an M-by-1 string array or cell array, then newStr is an M-by-N array.

  • If str is a 1-by-M string array or cell array, then newStr is an 1-by-M-by-N array.

For a string array or cell array of any size, split orients the N substrings along the first trailing dimension with a size of 1.

If the number of substrings is not the same for every element of str, then call split in a for-loop to divide the elements of str one at a time.

example

newStr = split(str,delimiter) divides each element of str at the delimiters specified by delimiter. The output newStr does not include the delimiters.

example

newStr = split(str,delimiter,dim) divides each element of str into a vector oriented along the dimension specified by dim.

example

[newStr,matches] = split(___) additionally returns an array, matches, that contains all occurrences of delimiters at which the split function splits str. You can use this syntax with any of the input arguments of the previous syntaxes.

Examples

collapse all

Split names in a string array at whitespace characters. Then reorder the strings and join them so that the last names precede the first names.

Create a 3-by-1 string array containing names. Starting in R2017a, you can create strings using double quotes instead of the string function.

names = ["Mary Butler";
         "Santiago Marquez";
         "Diana Lee"]
names = 3x1 string array
    "Mary Butler"
    "Santiago Marquez"
    "Diana Lee"

Split names at whitespace characters, making it a 3-by-2 string array.

names = split(names)
names = 3x2 string array
    "Mary"        "Butler" 
    "Santiago"    "Marquez"
    "Diana"       "Lee"    

Switch the columns of names so that the last names are in the first column. Add a comma after each last name.

names = [names(:,2) names(:,1)];
names(:,1) = names(:,1) + ','
names = 3x2 string array
    "Butler,"     "Mary"    
    "Marquez,"    "Santiago"
    "Lee,"        "Diana"   

Join the last and first names. The join function places a space character between the strings it joins. After the join, names is a 3-by-1 string array.

names = join(names)
names = 3x1 string array
    "Butler, Mary"
    "Marquez, Santiago"
    "Lee, Diana"

Create a string that contains the path to a folder. Starting in R2017a, you can create strings using double quotes instead of the string function.

myPath = "/Users/jdoe/My Documents/Examples"
myPath = 
"/Users/jdoe/My Documents/Examples"

Split the path at the / character. split returns myFolders as a 5-by-1 string array. The first string is "" because myPath starts with the / character.

myFolders = split(myPath,"/")
myFolders = 5x1 string array
    ""
    "Users"
    "jdoe"
    "My Documents"
    "Examples"

Join myFolders into a new path with \ as the delimiter. Add C: as the beginning of the path.

myNewPath = join(myFolders,"\");
myNewPath = 'C:' + myNewPath
myNewPath = 
"C:\Users\jdoe\My Documents\Examples"

Create a string. Starting in R2017a, you can create strings using double quotes instead of the string function.

str = "A horse! A horse! My kingdom for a horse!"
str = 
"A horse! A horse! My kingdom for a horse!"

Split str at exclamation points and at whitespace characters. newStr is a 10-by-1 string array. The last string is an empty string, "", because the last character in str is a delimiter.

newStr = split(str,[" ","!"])
newStr = 12x1 string array
    "A"
    "horse"
    ""
    "A"
    "horse"
    ""
    "My"
    "kingdom"
    "for"
    "a"
    "horse"
    ""

Create a string array in which each element contains comma-delimited data about a patient. Starting in R2017a, you can create strings using double quotes instead of the string function.

patients = ["LastName,Age,Gender,Height,Weight";
            "Adams,47,F,64,123";
            "Jones,,,68,175";
            "King,,M,66,180";
            "Smith,38,F,63,118"]
patients = 5x1 string array
    "LastName,Age,Gender,Height,Weight"
    "Adams,47,F,64,123"
    "Jones,,,68,175"
    "King,,M,66,180"
    "Smith,38,F,63,118"

Split the string array. A pair of commas with nothing between them indicates missing data. When split divides on repeated delimiters, it returns empty strings as corresponding elements of the output array.

patients = split(patients,",")
patients = 5x5 string array
    "LastName"    "Age"    "Gender"    "Height"    "Weight"
    "Adams"       "47"     "F"         "64"        "123"   
    "Jones"       ""       ""          "68"        "175"   
    "King"        ""       "M"         "66"        "180"   
    "Smith"       "38"     "F"         "63"        "118"   

Create a 3-by-1 string array containing names. Starting in R2017a, you can create strings using double quotes instead of the string function.

names = ["Mary Butler";
         "Santiago Marquez";
         "Diana Lee"]
names = 3x1 string array
    "Mary Butler"
    "Santiago Marquez"
    "Diana Lee"

Split the array at whitespace characters. By default, split orients the output substrings along the first trailing dimension with a size of 1. Because names is a 3-by-1 string array, split orients the substrings along the second dimension of splitNames, that is, the columns.

splitNames = split(names)
splitNames = 3x2 string array
    "Mary"        "Butler" 
    "Santiago"    "Marquez"
    "Diana"       "Lee"    

To orient the substrings along the rows, or first dimension, specify the dimension after you specify the delimiter. splitNames is now a 2-by-3 string array, with the first names in the first row and the last names in the second row.

splitNames = split(names," ",1)
splitNames = 2x3 string array
    "Mary"      "Santiago"    "Diana"
    "Butler"    "Marquez"     "Lee"  

Create a string. Starting in R2017a, you can create strings using double quotes instead of the string function.

str = "bacon, lettuce, and tomato"
str = 
"bacon, lettuce, and tomato"

Split str on delimiters. Return the results of the split in a string array, and the delimiters in a second string array. When there is no text between consecutive delimiters, split returns an empty string.

[newStr,matches] = split(str,["and",","," "])
newStr = 7x1 string array
    "bacon"
    ""
    "lettuce"
    ""
    ""
    ""
    "tomato"

matches = 6x1 string array
    ","
    " "
    ","
    " "
    "and"
    " "

Join newStr and matches back together with the join function.

originalStr = join(newStr,matches)
originalStr = 
"bacon, lettuce, and tomato"

Input Arguments

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Data Types: string | char | cell

Delimiting substrings, specified as a string array, a character vector, or a cell array of character vectors. The substrings specified in delimiter do not appear in the output newStr.

Specify multiple delimiters in a string array or a cell array of character vectors. The split function splits str on the elements of delimiter. The order in which delimiters appear in delimiter does not matter unless multiple delimiters begin a match at the same character in str. In that case, the split function splits on the first matching delimiter in delimiter.

Example: split(str,{' ',',','--'}) splits str on spaces, commas, and pairs of consecutive dashes.

Data Types: string | char | cell

Dimension along which to split strings, specified as a positive integer. If dim is not specified, then the default is the last array dimension with a size that does not equal 1.

Output Arguments

collapse all

Substrings split out of original array, returned as a string array or a cell array of character vectors. If the input array str is a string array, then so is newStr. Otherwise, newStr is a cell array of character vectors.

Data Types: string

Identified delimiters, returned as a string array or a cell array of character vectors. If the input array str is a string array, then so is matches. Otherwise, matches is a cell array of character vectors.

matches always contains one fewer element than output newStr contains.

Data Types: string

Extended Capabilities

Introduced in R2016b

Was this topic helpful?