フィルターのクリア

How can I extract capital words from a string

4 ビュー (過去 30 日間)
Meenal
Meenal 2022 年 12 月 5 日
回答済み: Image Analyst 2022 年 12 月 5 日
Hello all My string is "a_PSTC_SSSA_MNG_epwcrrauut_A" I want to extract "a_PSTC_SSSA_MNG" Thanks in advance
  1 件のコメント
Rik
Rik 2022 年 12 月 5 日
編集済み: Rik 2022 年 12 月 5 日
Why is the initial a_ included? And why isn't the final _A included? What is the rule here?

サインインしてコメントする。

回答 (2 件)

Mathieu NOE
Mathieu NOE 2022 年 12 月 5 日
編集済み: Mathieu NOE 2022 年 12 月 5 日
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
str = "a_PSTC_SSSA_MNG_epwcrrauut_A"
ind = strfind(str,'_');
str = char(str);
str_out = string(str(1:ind(end-1)-1)) % output string
str_out = "a_PSTC_SSSA_MNG"

Image Analyst
Image Analyst 2022 年 12 月 5 日
Try this:
s = "a_PSTC_SSSA_MNG_epwcrrauut_A_Extrastuff"
s = "a_PSTC_SSSA_MNG_epwcrrauut_A_Extrastuff"
words = strsplit(s, '_')
words = 1×7 string array
"a" "PSTC" "SSSA" "MNG" "epwcrrauut" "A" "Extrastuff"
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
The word a is something else.
The word PSTC is all capitals. The word SSSA is all capitals. The word MNG is all capitals.
The word epwcrrauut is something else.
The word A is all capitals.
The word Extrastuff has the first letter capitalized.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by