www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/TestForEmptyStringsAndMissingValuesExample.m

    %% 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

%%
% 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 = ''

%%
% 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

%%
% 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)

%%
isempty(str)

%%
% 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 == ''

%% 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,'')

%%
% The |contains| function converts |''| to an empty string and finds it within |str|.
%
% Test if |str| starts with the empty string.
TF = startsWith(str,'')

%%
% 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
%%
strlength(str)

%%
count(str,'')

%%
% 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','')

%%
% 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,'','-')

%%
% 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)

%%
% 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)

%%
ismissing(str)

%%
% 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)

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