How to divide a string into substrings

2 vues (au cours des 30 derniers jours)
Fariha Komal
Fariha Komal le 27 Avr 2019
Commenté : Fariha Komal le 27 Avr 2019
Hi
How can I fragment a string into substring? For example
Str = 'ABCDEF'
How can i fragment the above string in into following :
'A', 'BCDEF'
'AB' , 'CDEF'
'ABC' , 'DEF'
'ABCD' , 'EF'
'ABCDE' , 'F'
and strore substrings into a struct?

Réponse acceptée

madhan ravi
madhan ravi le 27 Avr 2019
Modifié(e) : madhan ravi le 27 Avr 2019
Assuming your using version higher than 2016b of MATLAB:
Str='A':'F';
Z=repmat(Str,strlength(Str)-1,1);
f = @(x)regexp(""+x,'\w*','match');
ZZ=[f(tril(Z)),f(triu(Z,1))];
S=cell2struct(ZZ,{'First_Part','Second_Part'},2)
For older versions:
Str='ABCDEF';
Z=repmat(Str,strlength(Str)-1,1);
f = @(x)regexp(cellstr(x),'\w*','match');
ZZ=[f(tril(Z)),f(triu(Z,1))];
S=cell2struct(reshape(ZZ,[],2),{'First_Part','Second_Part'},2)
  1 commentaire
Fariha Komal
Fariha Komal le 27 Avr 2019
Thank you Madhan Ravi for the great help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by