How can I extract capital words from a string
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello all My string is "a_PSTC_SSSA_MNG_epwcrrauut_A" I want to extract "a_PSTC_SSSA_MNG" Thanks in advance
1 commentaire
Réponses (2)
Mathieu NOE
le 5 Déc 2022
Modifié(e) : Mathieu NOE
le 5 Déc 2022
hello
this is one solution (for strings) . For char array it's almost the same (you don't even need to convert back from char to string as I did in the last line here)
str = "a_PSTC_SSSA_MNG_epwcrrauut_A" % input string
ind = strfind(str,'_');
str = char(str);
str_out = string(str(1:ind(end-1)-1)) % output string
0 commentaires
Image Analyst
le 5 Déc 2022
Try this:
s = "a_PSTC_SSSA_MNG_epwcrrauut_A_Extrastuff"
words = strsplit(s, '_')
for k = 1 : numel(words)
thisWord = words{k};
capWord = upper(thisWord);
if strcmp(thisWord, capWord)
fprintf('The word %s is all capitals.\n', thisWord);
elseif strcmp(thisWord(1), capWord(1))
fprintf('The word %s has the first letter capitalized.\n', thisWord);
else
fprintf('The word %s is something else.\n', thisWord);
end
end
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings 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!