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

30 次查看(过去 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