MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
To test whether a piece of text has no characters, the best practice is to use the
strlength function. You can use the same call whether the input is a string scalar or a
character vector.

str = "";
if strlength(str) == 0
disp('String has no text')
end

String has no text

chr = '';
if strlength(chr) == 0
disp('Character vector has no text')
end

Character vector has no text

Why Does Appending Strings Using Square Brackets Return
Multiple Strings?

You can append text to a character vector using square brackets. But if you add text to a
string array using square brackets, then the new text is concatenated as new elements of
the string array. To append text to strings, use the plus operator or the strcat function.

For example, if you concatenate two strings, then the result is a 1-by-2 string array.

str = ["Hello" "World"]

str = 1×2 string array
"Hello" "World"

However, if you concatenate two character vectors, then the result is a longer character
vector.

str = ['Hello' 'World']

chr = 'HelloWorld'

To append text to a string (or to the elements of a string array), use the plus operator
instead of square brackets.

str = "Hello" + "World"

str = "HelloWorld"

6 Characters and Strings

Free download pdf