# How can I determine whether a string contains a substring?

889 views (last 30 days)
MathWorks Support Team on 16 Jun 2017
Answered: walsh jupes on 28 Sep 2021
I have directory names that end in '0.0100'. I want to be able to skip certain directories by comparing the substring '0.0100' to the directory name. How can I do this?

MathWorks Support Team on 10 Jan 2020
Edited: MathWorks Support Team on 2 Mar 2020
You can use the 'contains' function to determine whether a string contains a given substring or not. Specifically, 'contains' returns true if the first argument contains the second argument and false otherwise. For example:
>> smallSubstring = '0.0100';
>> largeString1 = 'Item0.0100';
>> largeString2 = 'Item0.0101';
>> contains(largeString1, smallSubstring)
ans =
logical
1
>> contains(largeString2, smallSubstring)
ans =
logical
0
Praveen Manupati on 12 Jul 2019
contains(largeString1, smallSubstring)
The above line will also work and gives logical output similar to your example. Thanks!

Royi Avital on 26 Aug 2018
Edited: Royi Avital on 26 Aug 2018
I guess that since MATLAB R2016b it is recommended to use `contains` :
https://www.mathworks.com/help/matlab/ref/contains.html
KUMAR TRIPATHY on 9 Jan 2020
contains(largeString1, smallSubstring)

Praveen Manupati on 12 Jul 2019
contains(largeString1, smallSubstring)
The above line will also work and gives logical output. Thanks!

walsh jupes on 28 Sep 2021
Regular expression is widely used for pattern matching . Python has a built-in package called re , which can be used to work with Regular Expressions . The re module contains a function called search() , it can be used to check if a string contains the specified search pattern .
re.search(pattern, string, flags[optional])

### Tags

No tags entered yet.

R2017a

### Community Treasure Hunt

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

Start Hunting!