String arrays can contain both empty strings and missing values. Empty strings contain zero characters and display as double quotes with nothing between them (
""). You can determine if a string is an empty string using the
== operator. The empty string is a substring of every other string. Therefore, functions such as
contains always find the empty string within other strings. String arrays also can contain missing values. Missing values in string arrays display as
<missing>. To find missing values in a string array, use the
ismissing function instead of the
You can test a string array for empty strings using the
Starting in R2017a, you can create an empty string using double quotes with nothing between them (
""). Note that the size of
str is 1-by-1, not 0-by-0. However,
str contains zero characters.
str = ""
str = ""
Create an empty character vector using single quotes. Note that the size of
chr is 0-by-0. The character array
chr actually is an empty array, and not just an array with zero characters.
chr = ''
chr = 0x0 empty char array
Create an array of empty strings using the
strings function. Each element of the array is a string with no characters.
str2 = strings(1,3)
str2 = 1x3 string "" "" ""
str is an empty string by comparing it to an empty string.
if (str == "") disp 'str has zero characters' end
str has zero characters
Do not use the
isempty function to test for empty strings. A string with zero characters still has a size of 1-by-1. However, you can test if a string array has at least one dimension with a size of zero using the
Create an empty string array using the
strings function. To be an empty array, at least one dimension must have a size of zero.
str = strings(0,3)
str = 0x3 empty string array
str using the
ans = logical 1
Test a string array for empty strings. The
== operator returns a logical array that is the same size as the string array.
str = ["Mercury","","Apollo"]
str = 1x3 string "Mercury" "" "Apollo"
str == ''
ans = 1x3 logical array 0 1 0
Strings always contain the empty string as a substring. In fact, the empty string is always at both the start and the end of every string. Also, the empty string is always found between any two consecutive characters in a string.
Create a string. Then test if it contains the empty string.
str = "Hello, world"; TF = contains(str,"")
TF = logical 1
str starts with the empty string.
TF = startsWith(str,"")
TF = logical 1
Count the number of characters in
str. Then count the number of empty strings in
count function counts empty strings at the beginning and end of
str, and between each pair of characters. Therefore if
N characters, it also has
N+1 empty strings.
str = "Hello, world"
ans = 12
ans = 13
Replace a substring with the empty string. When you call
replace with an empty string, it removes the substring and replaces it with a string that has zero characters.
ans = "Hello, "
Insert a substring after empty strings using the
insertAfter function. Because there are empty strings between each pair of characters,
insertAfter inserts substrings between each pair.
ans = "-H-e-l-l-o-,- -w-o-r-l-d-"
In general, string functions that replace, erase, extract, or insert substrings allow you to specify empty strings as the starts and ends of the substrings to modify. When you do so, these functions operate on the start and end of the string, and between every pair of characters.
You can test a string array for missing values using the
ismissing function. The missing string is the string equivalent to
NaN for numeric arrays. It indicates where a string array has missing values. The missing string displays as
To create a missing string, convert a missing value using the
str = string(missing)
str = <missing>
You can create a string array with both empty and missing strings. Use the
ismissing function to determine which elements are strings with missing values. Note that the empty string is not a missing string.
str(1) = ""; str(2) = "Gemini"; str(3) = string(missing)
str = 1x3 string "" "Gemini" <missing>
ans = 1x3 logical array 0 0 1
str to a missing string. The comparison is always
false), even when you compare a missing string to another missing string.
str == string(missing)
ans = 1x3 logical array 0 0 0
To find missing strings, use the
ismissing function. Do not use the