part of file name
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
ayman mounir
am 18 Jul. 2019
Kommentiert: ayman mounir
am 19 Jul. 2019
If I have a file name such as ' Hello_World_2020 ', I want to take the part between the underscores ' world ', how I would do that.
Regards.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 18 Jul. 2019
Bearbeitet: Walter Roberson
am 18 Jul. 2019
s = 'Hello_World_2020';
c = regexp(s, '_', 'split') ;
c{2}
You can also use strsplit() instead of regexp. On the other hand, strsplit() invokes regexp internally.
3 Kommentare
Adam Danz
am 19 Jul. 2019
"If I write s=Hello_World_year_2020. the answer would be 'Hallo_year'. what I should modify?"
- "Hello_World_2020" --> "World": This removes everything before and after the underscores, including the underscores.
- "Hello_World_year_2020" --> "Hello_year": This doesnt' follow that rule.
I think Walter's solution will come in handy but you'll need to explain the rule you're following.
Weitere Antworten (3)
Adam Danz
am 18 Jul. 2019
s = 'Hello_World_2020';
c = regexp(s,'_(.*)_','tokens');
c = c{1}{1};
c =
'World'
madhan ravi
am 18 Jul. 2019
s=' Hello_World_2020 ';
[~,Wanted]=regexp(s,'_(.*)_','match','tokens');
Wanted{:}
2 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!