time | Calls | line |
---|
| | 1 | function joinedStr = strjoin(str, delimiter)
|
| | 2 | %STRJOIN Append elements of a string array
|
| | 3 | % S = STRJOIN(C) constructs S by linking the elements of C with a space
|
| | 4 | % between each element. C can be a string array or cell array of
|
| | 5 | % character vectors. S is a string scalar when C is a string array. S is
|
| | 6 | % a character vector when C is a cell array of character vectors.
|
| | 7 | %
|
| | 8 | % S = STRJOIN(C, DELIMITER) constructs S by linking each element of C
|
| | 9 | % with the elements of DELIMITER. DELIMITER can be either a string or a
|
| | 10 | % cell array of character vectors having one fewer element than C.
|
| | 11 | %
|
| | 12 | % If DELIMITER is a character vector it will interpret these escape
|
| | 13 | % sequences:
|
| | 14 | % \\ Backslash \n New line
|
| | 15 | % \0 Null \r Carriage return
|
| | 16 | % \a Alarm \t Horizontal tab
|
| | 17 | % \b Backspace \v Vertical tab
|
| | 18 | % \f Form feed
|
| | 19 | %
|
| | 20 | % If DELIMITER is a string array or cell array of character vectors, then
|
| | 21 | % all characters in DELIMITER are inserted as literal text, and escape
|
| | 22 | % characters are not interpreted.
|
| | 23 | %
|
| | 24 | % Examples:
|
| | 25 | %
|
| | 26 | % c = {'one', 'two', 'three'};
|
| | 27 | %
|
| | 28 | % % Join with space.
|
| | 29 | % strjoin(c)
|
| | 30 | % % 'one two three'
|
| | 31 | %
|
| | 32 | % % Join as a comma separated list.
|
| | 33 | % strjoin(c, ', ')
|
| | 34 | % % 'one, two, three'
|
| | 35 | %
|
| | 36 | % % Join with a cell array of character vectors DELIMITER.
|
| | 37 | % strjoin(c, {' + ', ' = '})
|
| | 38 | % % 'one + two = three'
|
| | 39 | %
|
| | 40 | % See also JOIN, SPLIT, STRCAT, STRSPLIT
|
| | 41 |
|
| | 42 | % Copyright 2012-2016 The MathWorks, Inc.
|
| | 43 |
|
< 0.001 | 2 | 44 | if nargin < 1 || nargin > 2
|
| | 45 | narginchk(1, 2);
|
< 0.001 | 2 | 46 | end
|
| | 47 |
|
< 0.001 | 2 | 48 | strIsString = isstring(str);
|
< 0.001 | 2 | 49 | strIsCellstr = iscellstr(str);
|
| | 50 |
|
| | 51 | % Check input arguments.
|
< 0.001 | 2 | 52 | if ~strIsCellstr && ~strIsString
|
| | 53 | error(message('MATLAB:strjoin:InvalidCellType'));
|
< 0.001 | 2 | 54 | end
|
| | 55 |
|
< 0.001 | 2 | 56 | numStrs = numel(str);
|
| | 57 |
|
< 0.001 | 2 | 58 | if nargin < 2
|
| | 59 | delimiter = {' '};
|
< 0.001 | 2 | 60 | elseif ischar(delimiter)
|
< 0.001 | 2 | 61 | delimiter = {strescape(delimiter)};
|
| | 62 | elseif iscellstr(delimiter) || isstring(delimiter)
|
| | 63 | numDelims = numel(delimiter);
|
| | 64 | if numDelims ~= 1 && numDelims ~= numStrs-1
|
| | 65 | error(message('MATLAB:strjoin:WrongNumberOfDelimiterElements'));
|
| | 66 | elseif strIsCellstr && isstring(delimiter)
|
| | 67 | delimiter = cellstr(delimiter);
|
| | 68 | end
|
| | 69 | delimiter = reshape(delimiter, numDelims, 1);
|
| | 70 | else
|
| | 71 | error(message('MATLAB:strjoin:InvalidDelimiterType'));
|
< 0.001 | 2 | 72 | end
|
| | 73 |
|
< 0.001 | 2 | 74 | str = reshape(str, numStrs, 1);
|
| | 75 |
|
< 0.001 | 2 | 76 | if strIsString
|
| | 77 | if isempty(str)
|
| | 78 | joinedStr = string('');
|
| | 79 | else
|
| | 80 | joinedStr = join(str, delimiter);
|
| | 81 | end
|
< 0.001 | 2 | 82 | elseif numStrs == 0
|
| | 83 | joinedStr = '';
|
< 0.001 | 2 | 84 | else
|
< 0.001 | 2 | 85 | joinedCell = cell(2, numStrs);
|
< 0.001 | 2 | 86 | joinedCell(1, :) = str;
|
< 0.001 | 2 | 87 | joinedCell(2, 1:numStrs-1) = delimiter;
|
| | 88 |
|
< 0.001 | 2 | 89 | joinedStr = [joinedCell{:}];
|
< 0.001 | 2 | 90 | end
|
< 0.001 | 2 | 91 | end
|
Other subfunctions in this file are not included in this listing.