how to combine strfind and index in one line?
2 次查看(过去 30 天)
显示 更早的评论
I tried to run strfind(argument,argument)(1) to return the first hit position, but matlab returns a error. It seems that I need to split the command into two lines? right?Is there any other way to make it in one line?
0 个评论
采纳的回答
Walter Roberson
2017-3-3
second_last = @(V) V(end-1);
positionLast2nd = second_last(strfind(newname, '_'));
It is possible to do it without the helper function, but it really is not worth doing except to prove that you can, because the code gets obscure.
0 个评论
更多回答(2 个)
Chad Greene
2017-3-3
编辑:Chad Greene
2017-3-3
You could use regexp with the 'once' option. For example, for this string:
str = ['Soldiers! Don''t give yourselves to brutes - men who despise you, enslave you, who '...
'regiment your lives, tell you what to do, what to think or what to feel! Who drill you,'...
'diet you, treat you like cattle, use you as cannon fodder.']
Each instance of the word you begins at the following indices:
regexp(str,'you')
ans =
22 61 74 92 109 166 176 187 208
If you only want the first instance use the 'once' option:
regexp(str,'you','once')
ans =
22
2 个评论
raym
2017-3-3
编辑:Walter Roberson
2017-3-3
1 个评论
Walter Roberson
2017-3-3
We distinctly recommend against using eval. eval() often leads to subtle bugs that show up only sometimes and are hard to find. It is also a big security risk.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!