Main Content

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. 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 Text in String and Character Arrays or watch Using String Arrays for Text Data.

Functions

expand all

String Arrays

stringString array
stringsCreate string array with no characters
joinCombine strings
plusAdd numbers, append strings

Character Arrays

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

Character or String Arrays

composeFormat data into multiple strings
sprintfFormat data into string or character vector
strcatConcatenate strings horizontally
appendCombine strings (Since R2019a)

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

Convert Between Numeric and Strings

doubleDouble-precision arrays
stringString array
str2doubleConvert strings to double precision values
num2strConvert numbers to character array

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

strlengthLengths of strings
isstrpropDetermine which characters in input strings are of specified category
isletterDetermine which characters are letters
isspaceDetermine which characters are space characters

Find

containsDetermine if pattern is in strings
matchesDetermine if pattern matches strings (Since R2019b)
countCount occurrences of pattern in strings
endsWithDetermine if strings end with pattern
startsWithDetermine if strings start with pattern
strfindFind strings within other strings
sscanfRead formatted data from strings

Replace

replaceFind and replace one or more substrings
replaceBetweenReplace substrings between start and end points
strrepFind and replace substrings

Building Patterns

patternPatterns to search and match text (Since R2020b)

Character-Matching Patterns

alphanumericsPatternMatch letter and digit characters (Since R2020b)
characterListPatternMatch characters from list (Since R2020b)
digitsPattern Match digit characters (Since R2020b)
lettersPatternMatch letter characters (Since R2020b)
whitespacePatternMatch whitespace characters (Since R2020b)
wildcardPatternMatches as few characters of any type (Since R2020b)

Pattern Search Rules

optionalPatternMake pattern optional to match (Since R2020b)
possessivePatternMatch pattern without backtracking (Since R2020b)
caseSensitivePatternMatch pattern with case sensitivity (Since R2020b)
caseInsensitivePatternMatch pattern regardless of case (Since R2020b)
asFewOfPatternMatch pattern as few times as possible (Since R2020b)
asManyOfPatternMatch pattern as many times as possible (Since R2020b)

Boundary Patterns

alphanumericBoundaryMatch boundary between alphanumeric and non-alphanumeric characters (Since R2020b)
digitBoundaryMatch boundary between digit characters and non-digit characters (Since R2020b)
letterBoundaryMatch boundary between letter characters and non-letter characters (Since R2020b)
whitespaceBoundaryMatch boundary between whitespace characters and non-whitespace characters (Since R2020b)
lineBoundaryMatch start or end of line (Since R2020b)
textBoundaryMatch start or end of text (Since R2020b)
lookAheadBoundaryMatch boundary before specified pattern (Since R2020b)
lookBehindBoundaryMatch boundary following specified pattern (Since R2020b)

Custom Pattern Display

maskedPatternPattern with specified display name (Since R2020b)
namedPatternDesignate named pattern (Since R2020b)

Regular Expressions

regexpMatch regular expression (case sensitive)
regexpiMatch regular expression (case insensitive)
regexprepReplace text using regular expression
regexptranslateTranslate text into regular expression
regexpPatternPattern that matches specified regular expression (Since R2020b)
joinCombine strings
plusAdd numbers, append strings
splitSplit strings at delimiters
splitlinesSplit strings at newline characters
strjoinJoin strings in array
strsplitSplit string or character vector at specified delimiter
strtokSelected parts of strings
extractExtract substrings from strings (Since R2020b)
extractAfterExtract substrings after specified positions
extractBeforeExtract substrings before specified positions
extractBetweenExtract substrings between start and end points
eraseDelete substrings within strings
eraseBetweenDelete substrings between start and end points
extractExtract substrings from strings (Since R2020b)
extractAfterExtract substrings after specified positions
extractBeforeExtract substrings before specified positions
extractBetweenExtract substrings between start and end points
insertAfterInsert strings after specified substrings
insertBeforeInsert strings before specified substrings
padAdd leading or trailing characters to strings
stripRemove leading and trailing characters from strings
lowerConvert strings to lowercase
upperConvert strings to uppercase
reverseReverse order of characters in strings
deblankRemove trailing whitespace from ends of strings
strtrimRemove leading and trailing whitespace from strings
strjustJustify strings
matchesDetermine if pattern matches strings (Since R2019b)
strcmpCompare strings
strcmpiCompare strings (case insensitive)
strncmpCompare first n characters of strings (case sensitive)
strncmpiCompare first n characters of strings (case insensitive)

Topics