Is there an efficient method to remove leading underscores from a cell array of strings?

조회 수: 28 (최근 30일)
Given
a={'rose','_tulip_blue','lilac','_daisy'}
is there an efficient way of stripping the leading underscores to give
b={'rose','tulip_blue','lilac','daisy'}
I tried
strtok(a,'_')
but that gives
b={'rose','tulip','lilac','daisy'}
and misses the '_blue'. I would like to avoid looping through the strings one at a time if possible.

채택된 답변

Adam
Adam 2014년 8월 6일
[b, c] = strtok( a, '_' )
b = strcat( b, c )
looks like it works.
  댓글 수: 1
Ken Campbell
Ken Campbell 2014년 8월 6일
I agree it works :-)
It also copes with more than 2 underscores. For example.
a={'rose','_tulip_blue_red','lilac','_daisy'}
[b,c]=strtok(a,'_');
b=strcat(b,c);
gives
b={'rose' 'tulip_blue_red' 'lilac' 'daisy'}
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

AJ von Alt
AJ von Alt 2014년 8월 6일
b = regexprep(a,'^_','','emptymatch')

카테고리

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