How can I determine whether a string contains a substring?
81 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 16 de Jun. de 2017
Editada: MathWorks Support Team
el 29 de Nov. de 2022
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?
Respuesta aceptada
MathWorks Support Team
el 17 de Nov. de 2022
Editada: MathWorks Support Team
el 17 de Nov. de 2022
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
You may find more information about 'contains' at the following documentation page:
1 comentario
Steven Lord
el 17 de Nov. de 2022
If you only want to detect a substring at the end of the larger string, use endsWith instead of contains. The corresponding function for detecting substrings at the beginning is startsWith.
A = ["MATLAB", "laboratory", "collaboration"];
startsWith(A, "lab")
endsWith(A, "lab", 'IgnoreCase', true) % LAB is close enough to lab
contains(A, "lab")
Más respuestas (1)
Royi Avital
el 26 de Ag. de 2018
Editada: MathWorks Support Team
el 29 de Nov. de 2022
0 comentarios
Ver también
Categorías
Más información sobre Software Development Tools en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!