is_str_in_str(c, str_match, is_case_sensitive)

Finds whether a string (or elements of a cell-array of strings) contains specified sub-strings.
121 Downloads
Updated 20 Jul 2015

View License

IS_STR_IN_STR finds whether a string (or elements of a cell-array of strings) contains at least one of a specified sub-string (or any of the elements of a cell-array of sub-strings).
L = IS_STR_IN_STR(X,PATTERN) returns a logical the same size as X indicating whether the pattern is in X.
X can be a string or cell-array of strings.
PATTERN can be a string*, cell-array of strings*, or a regular expression.
* Warning: REGEXP is used to find the occurences of strings so, for these, PATTERN should not include any regexp control characters (e.g. | or \ etc.).
L = IS_STR_IN_STR(X,PATTERN,TRUE) does the comparison as case-sensitive (otherwise the default is case-insensitive).
Example use:
cell_test = {'Black cat'; 'Cat and Dog'; 'white dog'; 'black dog'; 'blue bird'}
idx1 = is_str_in_str(cell_test, 'DOG');
idx2 = is_str_in_str(cell_test, 'dog', 1); %Case sensitive
idx3 = is_str_in_str(cell_test, {'black', 'blue'}); % Find cells with black OR blue
idx4 = is_str_in_str(cell_test, 'bla|blue'); % Find cells with black OR blue (specified via a regular expression).
[cell_test num2cell([idx1 idx2 idx3 idx4])]

Cite As

Roger Parkyn (2024). is_str_in_str(c, str_match, is_case_sensitive) (https://www.mathworks.com/matlabcentral/fileexchange/52175-is_str_in_str-c-str_match-is_case_sensitive), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Changed the help info at the top of the file and the Description for FileExchange.

1.0.0.0