Main Content

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 ascontainsalways 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 theismissingfunction instead of the==operator.

Test for Empty Strings

You can test a string array for empty strings using the==operator.

You can create an empty string using double quotes with nothing between them (""). Note that the size ofstris 1-by-1, not 0-by-0. However,strcontains zero characters.

str =""
str = ""

Create an empty character vector using single quotes. Note that the size ofchris 0-by-0. The character arraychractually 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 thestringsfunction. Each element of the array is a string with no characters.

str2 = strings(1,3)
str2 =1x3 string"" "" ""

Test ifstris 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 theisemptyfunction 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 theisemptyfunction.

Create an empty string array using thestringsfunction. To be an empty array, at least one dimension must have a size of zero.

str = strings(0,3)
str = 0x3 empty string array

Teststrusing theisemptyfunction.

isempty(str)
ans =logical1

Test a string array for empty strings. The==操作符返回一个逻辑数组is the same size as the string array.

str = ["Mercury","","Apollo"]
str =1x3 string"Mercury" "" "Apollo"
str ==''
ans =1x3 logical array0 1 0

Find Empty Strings Within Other Strings

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 =logical1

Test ifstrstarts with the empty string.

TF = startsWith(str,"")
TF =logical1

Count the number of characters instr. Then count the number of empty strings instr. Thecountfunction counts empty strings at the beginning and end ofstr, and between each pair of characters. Therefore ifstrhasNcharacters, it also hasN+1empty strings.

str
str = "Hello, world"
strlength(str)
ans = 12
count(str,"")
ans = 13

Replace a substring with the empty string. When you callreplacewith an empty string, it removes the substring and replaces it with a string that has zero characters.

replace(str,"world","")
ans = "Hello, "

Insert a substring after empty strings using theinsertAfterfunction. Because there are empty strings between each pair of characters,insertAfterinserts substrings between each pair.

insertAfter(str,"","-")
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.

Test for Missing Values

You can test a string array for missing values using theismissingfunction. The missing string is the string equivalent toNaNfor 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 thestringfunction.

str = string(missing)
str = 

You can create a string array with both empty and missing strings. Use theismissingfunction 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" 
ismissing(str)
ans =1x3 logical array0 0 1

Comparestrto a missing string. The comparison is always0(false), even when you compare a missing string to another missing string.

str == string(missing)
ans =1x3 logical array0 0 0

To find missing strings, use theismissingfunction. Do not use the==operator.

See Also

||||||||||||||||||

Related Topics