필터 지우기
필터 지우기

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

조회 수: 2 (최근 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
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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by