Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string startWith in Matlab

I would like to get only the begining of string , is there an equivalent matlab that allows to say : startsWith('It-is') like in java?

Thanks

like image 490
lola Avatar asked Apr 30 '12 11:04

lola


2 Answers

You can use the strfind function to tell if one string starts with another. The function returns the starting index of each occurrence of the string you're looking for, or an empty array if the string is not found.

S = 'Find the starting indices of the pattern string';
strfind(S, 'It-is')

If the string started with 'It-is' then the first index of the array returned by strfind would be 1 (i.e. the index of the first character).

like image 127
Bill the Lizard Avatar answered Sep 19 '22 05:09

Bill the Lizard


For long strings, it is faster to do this

s = (numel(a)>=numel(b)) && all(a(1:min(numel(a),numel(b)))==b(1:min(numel(a),numel(b))));

in order to have the equivalent to a.startsWith(b).

like image 45
S. Piérard Avatar answered Sep 21 '22 05:09

S. Piérard