This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Characters and Strings

Text in character arrays and string arrays

Character arrays and string arrays provide storage for text data in MATLAB®.

  • A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store short pieces of text as character vectors, such as c = 'Hello World'.

  • A string array is a container for pieces of text. String arrays provide a set of functions for working with text as data. Starting in R2017a, you can create strings using double quotes, such as str = "Greetings friend". To convert data to string arrays, use the string function.

For more information, see Represent Text with Character and String Arrays or watch Using String Arrays for Text Data.


expand all

String Arrays

stringString array
stringsCreate array of strings with no characters
joinCombine strings

Character Arrays

charCharacter array
cellstrConvert to cell array of character vectors
blanksCreate character array of blanks
newlineCreate newline character

Character or String Arrays

composeConvert data into formatted string array
sprintfFormat data into string
strcatConcatenate strings horizontally

Convert Input Arguments

convertCharsToStringsConvert character arrays to string arrays, leaving other arrays unaltered
convertStringsToCharsConvert string arrays to character arrays, leaving other arrays unaltered
convertContainedStringsToCharsConvert string arrays at any level of cell array or structure

Data Type

ischarDetermine if input is character array
iscellstrDetermine if input is cell array of character vectors
isstringDetermine if input is string array
isStringScalar Determine if input is string array with one element

Text Properties

strlengthLength of strings in string array
isstrpropDetermine if string is of specified category
isletterDetermine which characters are letters
isspaceDetermine which characters are space characters


containsDetermine if pattern is in string
countCount occurrences of pattern in string
endsWithDetermine if string ends with pattern
startsWithDetermine if string starts with pattern
strfindFind one string within another
sscanfRead formatted data from string


replaceFind and replace substrings in string array
replaceBetweenReplace substrings identified by indicators that mark their starts and ends
strrepFind and replace substring
joinCombine strings
splitSplit strings in string array
splitlinesSplit string at newline characters
strjoinJoin text in array
strsplitSplit string at specified delimiter
strtokSelected parts of string
eraseDelete substrings within strings
eraseBetweenDelete substrings between indicators that mark starts and ends of substrings
extractAfterExtract substring after specified position
extractBeforeExtract substring before specified position
extractBetweenExtract substrings between indicators that mark starts and ends of substrings
insertAfterInsert string after specified substring
insertBeforeInsert string before specified substring
padAdd leading or trailing characters to strings
stripRemove leading and trailing characters from string
lowerConvert string to lowercase
upperConvert string to uppercase
reverseReverse order of characters in string
deblankRemove trailing whitespace from end of string or character array
strtrimRemove leading and trailing whitespace from string array or character array
strjustJustify string or character array
strcmpCompare strings
strcmpiCompare strings (case insensitive)
strncmpCompare first n characters of strings (case sensitive)
strncmpiCompare first n characters of strings (case insensitive)
regexpMatch regular expression (case sensitive)
regexpiMatch regular expression (case insensitive)
regexprepReplace text using regular expression
regexptranslateTranslate text into regular expression


Represent Text with Character and String Arrays

Store and manipulate text using either character arrays or string arrays.

Analyze Text Data with String Arrays

This example shows how to analyze text data with string arrays. It shows how to store, split, and sort text, and how to compute and collect statistics for text in a string array.

Update Your Code to Accept Strings

Adopt string arrays as a text data type in your own code, following these guidelines.

Test for Empty Strings and Missing Values

Test string arrays for empty strings and missing values.

Formatting Text

Compose character arrays or string arrays that include ordinary text and data formatted to your specification.

Search and Replace Text

MATLAB provides several functions to search for, replace, or extract text in string arrays and character vectors.

Compare Text

Compare and inspect character arrays and string arrays.

Regular Expressions

Regular expressions are a versatile way to search and replace text.