MATLAB 在字符串元胞数组的每个字符串末尾添加一个字母
MATLAB add a letter at the end of every string of a cell array of string
假设我有一个字符串元胞数组:
A = {'hello','world','how','are','you'};
我想在每个字符串的末尾添加字母z
,以获得:
Az = {'helloz','worldz','howz','arez','youz'};
我正在使用 for 循环来完成此任务,但我想尽可能地改进它。
这是我目前使用的代码:
Az = cell(size(A)); % Preload
for i = 1:size(A,2)
Az{i} = [A{i},'z'];
end
有什么建议吗?
strcat
就是这样做的:
Az = strcat(A, 'z');
s = strcat(s1,...,sN)
horizontally concatenates strings s1
,...,sN
. Each input argument can be a single string, a collection of strings in a cell array, or a collection of strings in a character array.
If any input argument is a cell array, the result is a cell array of strings. Otherwise, the result is a character array.
For character array inputs, strcat
removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form feed. For cell array inputs, strcat does not remove trailing white space.
假设我有一个字符串元胞数组:
A = {'hello','world','how','are','you'};
我想在每个字符串的末尾添加字母z
,以获得:
Az = {'helloz','worldz','howz','arez','youz'};
我正在使用 for 循环来完成此任务,但我想尽可能地改进它。
这是我目前使用的代码:
Az = cell(size(A)); % Preload
for i = 1:size(A,2)
Az{i} = [A{i},'z'];
end
有什么建议吗?
strcat
就是这样做的:
Az = strcat(A, 'z');
s = strcat(s1,...,sN)
horizontally concatenates stringss1
,...,sN
. Each input argument can be a single string, a collection of strings in a cell array, or a collection of strings in a character array.If any input argument is a cell array, the result is a cell array of strings. Otherwise, the result is a character array.
For character array inputs,
strcat
removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form feed. For cell array inputs, strcat does not remove trailing white space.