【问题标题】:MATLAB: Find locations of multiple characters inside stringMATLAB:在字符串中查找多个字符的位置
【发布时间】:2020-09-30 03:49:11
【问题描述】:

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

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。

我们将不胜感激。 谢谢。

【问题讨论】:

    标签: string matlab find location


    【解决方案1】:

    您可以使用ismemberfind

    查找倒数第二个位置:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-24
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 2012-08-03
      相关资源
      最近更新 更多