How can I determine whether a string contains a substring?

Illustration
Alan Garger - 2023-12-27T15:23:42+00:00
Question: How can I determine whether a string contains a substring?

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 answered . 2025-11-20

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

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!