MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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 = logical
1


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 =
"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","")


Test for Empty Strings and Missing Values
Free download pdf