from
C-Style String Comparisons
by Rahul PN
Implements C-style string comparison operations
|
| cstrcmpi(x, y)
|
function res = cstrcmpi(x, y)
% res = cstrcmpi(x, y) gives the results of the comparison ignoring case.
% "res" is zero if the strings are identical.
% "res" is positive if string "x" is greater than string "y",
% and is negative if string "y" is greater than string "x".
% Comparisons of are made according to the ASCII values.
res = 0;
if(iscellstr(x))
x = char(x);
end
if(iscellstr(y))
y = char(y);
end
if(~ischar(x) || ~ischar(y))
error('Input must be character arrays');
end
x = lower(x);
y = lower(y);
lenx = length(x);
leny = length(y);
strLen = min(lenx, leny);
%Find the first index where a mismatch occures
i = find( x(1 : strLen) ~= y(1 : strLen), 1);
if(~isempty(i))
res = double(x(i)) - double(y(i));
end
if(res == 0 && (lenx ~= leny))
if(strLen == lenx)
res = -(double(y(strLen + 1)));
else
res = -(double(x(strLen + 1)));
end
end
|
|
Contact us at files@mathworks.com