time | Calls | line |
|---|
| | 1 | function varargout = split(str, varargin)
|
| | 2 | %SPLIT Split strings in string array
|
| | 3 | % NEWSTR = SPLIT(STR) splits STR at whitespace into the output array
|
| | 4 | % NEWSTR. If STR is a string scalar or character vector, then NEWSTR is a
|
| | 5 | % P-by-1 string array or cell array where P is the number of splits.
|
| | 6 | % Whitespace is defined as any sequence of whitespace characters such as
|
| | 7 | % spaces, tabs, and newlines.
|
| | 8 | %
|
| | 9 | % STR can be a string array, character vector, or cell array of character
|
| | 10 | % vectors. If STR is a string array then NEWSTR is a string array. Otherwise,
|
| | 11 | % NEWSTR is a cell array of character vectors.
|
| | 12 | %
|
| | 13 | % If STR is an M-by-1 string vector, then the size of NEWSTR is M-by-P.
|
| | 14 | % If STR is 1-by-N, then NEWSTR is 1-by-N-by-P. If STR is a strings
|
| | 15 | % M-by-N-by-... array, NEWSTR is a string array with an additional
|
| | 16 | % dimension containing the number of splits P from each element in STR.
|
| | 17 | % The size of NEWSTR is M-by-N-by-...-by-P.
|
| | 18 | %
|
| | 19 | % If the number of splits P is not the same for every element in STR,
|
| | 20 | % then SPLIT will error. In that case, write a FOR loop to call SPLIT on
|
| | 21 | % each element of STR.
|
| | 22 | %
|
| | 23 | % NEWSTR = SPLIT(STR,DELIMITER) splits STR at DELIMITER into NEWSTR.
|
| | 24 | % DELIMITER can be a string array, a character vector, a cell array of
|
| | 25 | % character vectors, or a pattern array. If DELIMITER is a string array,
|
| | 26 | % a cell array, or a pattern array, SPLIT splits STR along the elements in
|
| | 27 | % DELIMITER, in the order in which they appear in the DELIMITER array.
|
| | 28 | %
|
| | 29 | % NEWSTR = SPLIT(STR,DELIMITER,DIM) splits STR at DELIMITER into NEWSTR
|
| | 30 | % along the specified dimension. The default value of DIM is the first
|
| | 31 | % trailing dimension equal to 1.
|
| | 32 | %
|
| | 33 | % [NEWSTR,MATCHES] = SPLIT(...) returns the string array, MATCHES,
|
| | 34 | % containing all occurrences of the delimiters at which STR was split.
|
| | 35 | % MATCHES always contains one fewer element than NEWSTR along the
|
| | 36 | % dimension P.
|
| | 37 | %
|
| | 38 | % Examples:
|
| | 39 | %
|
| | 40 | % STR = ["Mary Jones";"John Smith";"Elizabeth Young"];
|
| | 41 | % split(STR)
|
| | 42 | %
|
| | 43 | % returns
|
| | 44 | %
|
| | 45 | % "Mary" "Jones"
|
| | 46 | % "John" "Smith"
|
| | 47 | % "Elizabeth" "Young"
|
| | 48 | %
|
| | 49 | % STR = ["Mary Jones";"John Smith";"Elizabeth Young"];
|
| | 50 | % split(STR,' ',1)
|
| | 51 | %
|
| | 52 | % returns
|
| | 53 | %
|
| | 54 | % "Mary" "John" "Elizabeth"
|
| | 55 | % "Jones" "Smith" "Young"
|
| | 56 | %
|
| | 57 | % STR = 'Edgar Allen Poe';
|
| | 58 | % split(STR)
|
| | 59 | %
|
| | 60 | % returns
|
| | 61 | % 'Edgar'
|
| | 62 | % 'Allen'
|
| | 63 | % 'Poe'
|
| | 64 | %
|
| | 65 | % myPath = "/Users/jdoe/My Documents/Examples";
|
| | 66 | % myFolders = split(myPath,'/')
|
| | 67 | %
|
| | 68 | % returns
|
| | 69 | %
|
| | 70 | % ""
|
| | 71 | % "Users"
|
| | 72 | % "jdoe"
|
| | 73 | % "My Documents"
|
| | 74 | % "Examples"
|
| | 75 | %
|
| | 76 | % See also SPLITLINES, JOIN, COMPOSE, NEWLINE, COUNT, STRING, PATTERN
|
| | 77 |
|
| | 78 | % Copyright 2015-2020 The MathWorks, Inc.
|
| | 79 |
|
< 0.001 | 1 | 80 | narginchk(1, 3);
|
< 0.001 | 1 | 81 | if ~isTextStrict(str)
|
| | 82 | firstInput = getString(message('MATLAB:string:FirstInput'));
|
| | 83 | error(message('MATLAB:string:MustBeCharCellArrayOrString', firstInput));
|
< 0.001 | 1 | 84 | end
|
| | 85 |
|
< 0.001 | 1 | 86 | try
|
< 0.001 | 1 | 87 | s = string(str);
|
< 0.001 | 1 | 88 | [varargout{1:nargout}] = s.split(varargin{:});
|
| | 89 |
|
< 0.001 | 1 | 90 | if ~isstring(str)
|
| | 91 | varargout{1} = cellstr(varargout{1});
|
| | 92 |
|
| | 93 | if nargout > 1
|
| | 94 | varargout{2} = cellstr(varargout{2});
|
| | 95 | end
|
< 0.001 | 1 | 96 | end
|
| | 97 |
|
| | 98 | catch E
|
| | 99 | throw(E)
|
< 0.001 | 1 | 100 | end
|
< 0.001 | 1 | 101 | end
|
Other subfunctions in this file are not included in this listing.