Test for Empty Strings and Missing Values
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. To find
missing values in a string array, use the ismissing function instead of the == operator.
Test for Empty Strings
You can test a string array for empty strings using the == operator.
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
""
""
""
Test if 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 isempty function.
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)
6 Characters and Strings
6-20
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 values in a string array, use the ismissing function instead of the == operator.
Test for Empty Strings
You can test a string array for empty strings using the == operator.
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
""
""
""
Test if 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 isempty function.
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)
6 Characters and Strings
6-20
