How to extract the first part of an array of strings by pattern?

3 ビュー (過去 30 日間)
JFz
JFz 2017 年 5 月 25 日
コメント済み: Image Analyst 2017 年 5 月 25 日
Hi,
I have an array of strings: L_Name. Each string has several "_" in it. I would like to have an array that take the first part in each string by '_'. I tried to use strfind, or reg, but just cannot get it.
How to do that? Thanks! JF

採用された回答

Stephen23
Stephen23 2017 年 5 月 25 日
編集済み: Stephen23 2017 年 5 月 25 日
If you want the part of the string before the first underscore '_', then you can use regexp:
>> C = {'L_Name','Z_Y_X','Aaaa_BBBB','MMM'};
>> D = regexp(C,'[^_]+','once','match');
>> D{:}
ans = L
ans = Z
ans = Aaaa
ans = MMM
>>
  2 件のコメント
JFz
JFz 2017 年 5 月 25 日
Thanks.
Image Analyst
Image Analyst 2017 年 5 月 25 日
The nice thing is, it also works for an array of strings (the new string data type that MATLAB now has) in addition to the cell array Stephen showed:
C = ["L_Name","Z_Y_X","Aaaa_BBBB","MMM"]
D = regexp(C,'[^_]+','once','match');
D{:}

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

その他の回答 (0 件)

カテゴリ

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