SUBSTR Extract a substring out of a string. SUBSTR(STRING, OFFSET, LENGTH) extracts a substring out of STRING with given LENGTH starting at the given OFFSET. First character is at offset 0. If OFFSET is negative, starts that far from the end of the string. If LENGTH is omitted, returns everything to the end of the string. If LENGTH is negative, removes that many characters from the end of the string. SUBSTR(STRING, OFFSET, LENGTH, REPLACEMENT) will not return the substring as specified by STRING, OFFSET, and LENGTH (see above) but rather replace it by REPLACEMENT and return the result. Examples: Get first character: substr(string, 0, 1) Get last character: substr(string, -1, 1) Remove first character: substr(string, 1) Remove last character: substr(string, 0, -1) Remove first and last character: substr(string, 1, -1) SUBSTR is a MATLAB version of the Perl operator with the same name. However, unlike Perl's SUBSTR, no warning is produced if the substring is totally outside the string.
0001 function outstr = substr(str, offset, len, repl) 0002 %SUBSTR Extract a substring out of a string. 0003 % 0004 % SUBSTR(STRING, OFFSET, LENGTH) extracts a substring out of STRING with 0005 % given LENGTH starting at the given OFFSET. First character is at offset 0006 % 0. If OFFSET is negative, starts that far from the end of the string. 0007 % If LENGTH is omitted, returns everything to the end of the string. If 0008 % LENGTH is negative, removes that many characters from the end of the 0009 % string. 0010 % 0011 % SUBSTR(STRING, OFFSET, LENGTH, REPLACEMENT) will not return the 0012 % substring as specified by STRING, OFFSET, and LENGTH (see above) but 0013 % rather replace it by REPLACEMENT and return the result. 0014 % 0015 % Examples: 0016 % 0017 % Get first character: substr(string, 0, 1) 0018 % Get last character: substr(string, -1, 1) 0019 % Remove first character: substr(string, 1) 0020 % Remove last character: substr(string, 0, -1) 0021 % Remove first and last character: substr(string, 1, -1) 0022 % 0023 % SUBSTR is a MATLAB version of the Perl operator with the same name. 0024 % However, unlike Perl's SUBSTR, no warning is produced if the substring 0025 % is totally outside the string. 0026 0027 % Author: Peter J. Acklam 0028 % Time-stamp: 2003-03-31 18:19:53 +0200 0029 % E-mail: pjacklam@online.no 0030 % URL: http://home.online.no/~pjacklam 0031 0032 % Check number of input arguments. 0033 error(nargchk(2, 4, nargin)); 0034 0035 n = length(str); 0036 0037 % Get lower index. 0038 lb = offset + 1; % offset from beginning of string 0039 if offset < 0 0040 lb = lb + n; % offset from end of string 0041 end 0042 lb = max(lb, 1); 0043 0044 % Get upper index. 0045 if nargin == 2 % SUBSTR(STR, OFFSET) 0046 ub = n; 0047 elseif nargin > 2 % SUBSTR(STR, OFFSET, LEN) 0048 if len >= 0 0049 ub = lb + len - 1; 0050 else 0051 ub = n + len; 0052 end 0053 ub = min(ub, n); 0054 end 0055 0056 % Extract or replace substring. 0057 if nargin < 4 0058 outstr = str(lb : ub); % extract substring 0059 else 0060 outstr = [str(1:lb-1) repl str(ub+1:end)]; % replace substring 0061 end