Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Test for Empty Strings and Missing Values

Starting in R2016b, you can create string arrays that 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 == operator.

Test for Empty Strings

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

Create an empty string using the strings function. Note that the size of str is 1-by-1, not 0-by-0. However, str contains zero characters.

str = strings
str = 

  string

    ""

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 =

  0×0 empty char array

Test if str is an empty string by comparing it to an empty character vector. First the empty character vector is converted to a string with no characters. Then it is compared to the 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 and test it using isempty.

str = strings(0,3)
str = 

  0×3 empty string array

isempty(str)
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 = string({'Mercury','','Apollo'})
str = 

  1×3 string array

    "Mercury"    ""    "Apollo"

str == ''
ans =

  1×3 logical array

   0   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 = string('Hello, world');
TF = contains(str,'')
TF =

  logical

   1

The contains function converts '' to an empty string and finds it within str.

Test if 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 str. The count function counts empty strings at the beginning and end of str, and between each pair of characters. Therefore if str has N characters, it also has N+1 empty strings.

str
str = 

  string

    "Hello, world"

strlength(str)
ans =

    12

count(str,'')
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.

replace(str,'world','')
ans = 

  string

    "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.

insertAfter(str,'','-')
ans = 

  string

    "-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 the ismissing function. The missing string is the string equivalent to NaN for numeric arrays. It indicates where a string array has missing values. When you display a missing string, the result is <missing>, with no quotation marks.

To create a missing string, convert a NaN value using the string function.

str = string(nan)
str = 

  string

    <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) = string('');
str(2) = string('Gemini');
str(3) = string(nan)
str = 

  1×3 string array

    ""    "Gemini"    <missing>

ismissing(str)
ans =

  1×3 logical array

   0   0   1

Compare str to a missing string. The comparison is always 0 (false), even when you compare a missing string to another missing string.

str == string(nan)
ans =

  1×3 logical array

   0   0   0

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

See Also

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

Related Examples

Was this topic helpful?