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 ascharacter vectors, such asc = '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 asstr = "Greetings friend". To convert data to string arrays, use thestringfunction.

For more information, seeText in String and Character Arraysor watchUsing String Arrays for Text Data.

Functions

经验值and all

String Arrays

string String array
strings Create string array with no characters
join Combine strings
plus 添加数据,添加字符串

Character Arrays

char Character array
cellstr Convert to cell array of character vectors
blanks Create character array of blanks
newline Create newline character

Character or String Arrays

compose Format data into multiple strings
sprintf Format data into string or character vector
strcat Concatenate strings horizontally
append Combine strings

Convert Input Arguments

convertCharsToStrings Convert character arrays to string arrays, leaving other arrays unaltered
convertStringsToChars Convert string arrays to character arrays, leaving other arrays unaltered
convertContainedStringsToChars Convert string arrays at any level of cell array or structure

Convert Between Numeric and Strings

double Double-precision arrays
string String array
str2double Convert strings to double precision values
num2str Convert numbers to character array

Data Type

ischar Determine if input is character array
iscellstr Determine if input is cell array of character vectors
isstring Determine if input is string array
isStringScalar Determine if input is string array with one element

Text Properties

strlength Lengths of strings
isstrprop Determine which characters in input strings are of specified category
isletter Determine which characters are letters
isspace Determine which characters are space characters

Find

contains Determine if pattern is in strings
matches Determine if pattern matches strings
count Count occurrences of pattern in strings
endsWith Determine if strings end with pattern
startsWith Determine if strings start with pattern
strfind Find strings within other strings
sscanf Read formatted data from strings

Replace

replace Find and replace one or more substrings
replaceBetween Replace substrings between start and end points
strrep Find and replace substrings

Building Patterns

pattern Patterns to search and match text

Character-Matching Patterns

alphanumericsPattern Match letter and digit characters
characterListPattern Match characters from list
digitsPattern Match digit characters
lettersPattern Match letter characters
whitespacePattern Match whitespace characters
wildcardPattern Matches as few characters of any type

Pattern Search Rules

optionalPattern Make pattern optional to match
possessivePattern 匹配模式without backtracking
caseSensitivePattern 匹配模式with case sensitivity
caseInsensitivePattern 匹配模式regardless of case
asFewOfPattern 匹配模式as few times as possible
asManyOfPattern 匹配模式as many times as possible

Boundary Patterns

alphanumericBoundary Match boundary between alphanumeric and non-alphanumeric characters
digitBoundary Match boundary between digit characters and nondigit characters
letterBoundary Match boundary between letter characters and nonletter characters
whitespaceBoundary Match boundary between whitespace characters and non-whitespace characters
lineBoundary Match start or end of line
textBoundary Match start or end of text
lookAheadBoundary Match boundary before specified pattern
lookBehindBoundary Match boundary following specified pattern

Custom Pattern Display

maskedPattern Pattern with specified display name
namedPattern Designate named pattern

Regular Expressions

regexp Match regular expression (case sensitive)
regexpi Match regular expression (case insensitive)
regexprep Replace text using regular expression
regexptranslate Translate text into regular expression
regexpPattern Pattern that matches specified regular expression
join Combine strings
plus 添加数据,添加字符串
split Split strings at delimiters
splitlines Split strings at newline characters
strjoin Join strings in array
strsplit Split string or character vector at specified delimiter
strtok Selected parts of strings
extract Extract substrings from strings
extractAfter Extract substrings after specified positions
extractBefore Extract substrings before specified positions
extractBetween Extract substrings between start and end points
erase Delete substrings within strings
eraseBetween Delete substrings between start and end points
extract Extract substrings from strings
extractAfter Extract substrings after specified positions
extractBefore Extract substrings before specified positions
extractBetween Extract substrings between start and end points
insertAfter Insert strings after specified substrings
insertBefore Insert strings before specified substrings
pad Add leading or trailing characters to strings
strip Remove leading and trailing characters from strings
lower Convert strings to lowercase
upper Convert strings to uppercase
reverse Reverse order of characters in strings
deblank Remove trailing whitespace from ends of strings
strtrim Remove leading and trailing whitespace from strings
strjust Justify strings
matches Determine if pattern matches strings
strcmp Compare strings
strcmpi Compare strings (case insensitive)
strncmp Compare firstncharacters of strings (case sensitive)
strncmpi Compare firstncharacters of strings (case insensitive)

Topics

Text in String and Character Arrays

Store and manipulate text using either string arrays or character 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.

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.

Unicode and ASCII Values

MATLAB stores all characters as Unicode characters. Both strings and character vectors use the same encoding. You can convert characters to their Unicode code values, and numbers to characters.

Hexadecimal and Binary Values

Specify hexadecimal and binary values either as literals or as text. Hexadecimal and binary literals are stored as integers. You can convert text representing hexadecimal and binary values to numbers, and numbers to text representations.