How can I determine whether a string contains a substring?

Alan Garger · Dec 27, 2023 · 2.3K views
Question
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?  
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 13, 2026
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

 

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!