MATLAB:查找字符串中多个字符的位置

MATLAB: Find locations of multiple characters inside string

如何找到字符串中某些字符的位置。这是我的尝试:

Example = "Hello, this is Tom. I wonder, should I go run?";
SearchedCharacters = {'.','!',',','?'};
%Plan one
Locations = strfind(Example, SearchedCharacters);
%Plan two
Locations = cellfun(@(s)find(~cellfun('isempty',strfind(C,s))),SearchedCharacters,'uni',0);

我的两个计划都出错了。

终于。有了字符串中字符的位置,我想确定字符串中倒数第二个感兴趣的字符。在这种情况下,它将是“,”(就在“wonder”一词之后),位置 = 29.

我们将不胜感激。 谢谢

您可以使用 ismember and find.

找到倒数第二个位置:

Example = 'Hello, this is Tom. I wonder, should I go run?' ;
SearchedCharacters = '.!,?' ;
idx = ismember (Example, SearchedCharacters);
Loc = find (idx, 2, 'last');
if numel (Loc) < 2
    error ('the requested character cannot be found')
end
SecondLast = Loc (1);

查找所有位置:

Locations = find (idx);