tf = logical
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"
As an alternative, you can use the strcat function. strcat appends text whether the input
arguments are strings or character vectors.
str = strcat("Hello","World")
str = "HelloWorld"
Whether you use square brackets, plus, or strcat, you can specify an arbitrary number of
arguments. Append a space character between Hello and World.
str = "Hello" + " " + "World"
6 Characters and Strings
6-62
Précédent

- 244/1450

Suivant