string 在Matlab中查找字符串中的特定字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1425147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding a specific character in a string in Matlab
提问by stanigator
Suppose I have a string '[email protected]'
. I want to store the string before and after "@" into 2 separate strings. What would be the easiest method of finding the "@" character or other characters in the string?
假设我有一个字符串 '[email protected]'
。我想将“@”前后的字符串存储到 2 个单独的字符串中。在字符串中查找“@”字符或其他字符的最简单方法是什么?
采纳答案by stanigator
I used strtok and strrep from Matlab instead.
我改用了 Matlab 中的 strtok 和 strrep。
回答by gnovice
STRTOKand an index operation should do the trick:
STRTOK和索引操作应该可以解决问题:
str = '[email protected]';
[name,address] = strtok(str,'@');
address = address(2:end);
Or the last line could also be:
或者最后一行也可以是:
address(1) = '';
回答by Amro
You can use strread:
您可以使用strread:
str = '[email protected]';
[a b] = strread(str, '%s %s', 'delimiter','@')
a =
'johndoe'
b =
'hotmail.com'
回答by kwatford
For "easiest",
为了“最简单”,
>> email = '[email protected]'
email =
[email protected]
>> 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
It would be slightly more complicated if you were looking for something with more than one character, or you weren't sure if there was exactly one @, and in that case MATLAB has a lot of functions for searching through text, including regular expressions (see doc regexp
).
如果您要查找包含多个字符的内容,或者您不确定是否只有一个 @,则情况会稍微复杂一些,在这种情况下,MATLAB 有很多用于搜索文本的函数,包括正则表达式(见doc regexp
)。
回答by mtrw
TEXTSCANworks too.
TEXTSCAN也有效。
str = '[email protected]';
parts = textscan(str, '%s %s', 'Delimiter', '@');
returns a cell array where parts{1} is 'johndoe' and parts{2} is 'hotmail.com'.
返回一个元胞数组,其中parts{1} 是'johndoe',parts{2} 是'hotmail.com'。
回答by SnakeEyes1482
If this thread isn't completely enumerated by now, may I add another? A handy perl-based MATLAB function:
如果现在还没有完全列举这个线程,我可以添加另一个吗?一个方便的基于 perl 的 MATLAB 函数:
email = '[email protected]';
parts = regexp(email,'@', 'split');
parts is a two element cell array similar to mtrw's implementation of textscan. Maybe overkill, but regexp is much more useful when splitting a string by multiple delimiting characters or pattern searching. The only downside is the use of regular expressions which I still haven't mastered after 15 years of coding.
零件是一个两元素元胞数组,类似于 mtrw 的 textscan 实现。可能有点矫枉过正,但是当通过多个分隔字符或模式搜索拆分字符串时,regexp 更有用。唯一的缺点是正则表达式的使用,经过 15 年的编码,我仍然没有掌握。
回答by sachin
String email = "[email protected]";
String email = "[email protected]";
String a[] = email.split("@");
String def = null;
String ghi = null;
for(int i=0;i<a.length;i++){
def = a[0];
ghi = a[1];
}