Suppose I have a string called "[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?
+1
A:
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
).
kwatford
2009-09-15 04:51:00
+4
A:
STRTOK and an index operation should do the trick:
str = '[email protected]';
[name,address] = strtok(str,'@');
address = address(2:end);
Or the last line could also be:
address(1) = '';
gnovice
2009-09-15 06:05:06
+3
A:
TEXTSCAN works too.
str = '[email protected]';
parts = textscan(str, '%s %s', 'Delimiter', '@');
returns a cell array where parts{1} is 'johndoe' and parts{2} is 'hotmail.com'.
mtrw
2009-09-15 06:44:57
+5
A:
You can use strread:
str = '[email protected]';
[a b] = strread(str, '%s %s', 'delimiter','@')
a =
'johndoe'
b =
'hotmail.com'
Amro
2009-09-15 06:56:35