代码之家  ›  专栏  ›  技术社区  ›  stanigator

在matlab中查找字符串中的特定字符

  •  19
  • stanigator  · 技术社区  · 15 年前

    假设我有根线 'johndoe@hotmail.com' .我想将“@”前后的字符串存储为两个单独的字符串。在字符串中查找“@”字符或其他字符的最简单方法是什么?

    7 回复  |  直到 8 年前
        1
  •  17
  •   gnovice    15 年前

    STRTOK 索引操作应该做到这一点:

    str = 'johndoe@hotmail.com';
    [name,address] = strtok(str,'@');
    address = address(2:end);
    

    或者最后一行也可以是:

    address(1) = '';
    
        2
  •  12
  •   Amro    15 年前

    你可以使用 斯特莱德 :

    str = 'johndoe@hotmail.com';
    [a b] = strread(str, '%s %s', 'delimiter','@')
    a = 
        'johndoe'
    b = 
        'hotmail.com'
    
        3
  •  11
  •   kenm    15 年前

    为了“最简单”,

    >> email = 'johndoe@hotmail.com'
    email =
    johndoe@hotmail.com
    >> email == '@'
    ans =
      Columns 1 through 13
         0     0     0     0     0     0     0     1     0     0     0     0     0
      Columns 14 through 19
         0     0     0     0     0     0
    >> at = find(email == '@')
    at =
         8
    >> email(1:at-1)
    ans =
    johndoe
    >> email(at+1:end)
    ans =
    hotmail.com
    

    如果您要查找的对象不止一个字符,或者您不确定是否只有一个@,那么会稍微复杂一点,在这种情况下,matlab有许多用于搜索文本的函数,包括正则表达式(请参见 doc regexp )

        4
  •  7
  •   mtrw    15 年前

    TEXTSCAN 也工作。

    str = 'johndoe@hotmail.com';
    parts = textscan(str, '%s %s', 'Delimiter', '@');
    

    返回一个单元格数组,其中parts_1为“johndoe”,parts_2_为“hotmail.com”。

        5
  •  5
  •   Shai    12 年前

    如果现在还没有完全枚举这个线程,我可以再添加一个吗?一个基于Perl的Matlab函数:

    email = 'johndoe@hotmail.com';
    parts = regexp(email,'@', 'split');
    

    parts是一个两元素单元数组,类似于mtrw对textscan的实现。可能是杀伤力过大,但当通过多个分隔字符或模式搜索拆分字符串时,regexp更有用。唯一的缺点是正则表达式的使用,15年的编码之后我仍然没有掌握它。

        6
  •  -1
  •   stanigator    15 年前

    我使用了Matlab中的strtok和strrep。

        7
  •  -4
  •   sachin    12 年前

    string email=“johndoe@hotmail.com”;

        String a[] = email.split("@");
        String def = null;
        String ghi = null;
        for(int i=0;i<a.length;i++){
            def = a[0];
            ghi = a[1];
        }