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?
The strchr() function finds the first occurrence of a character in a string. The character c can be the null character (\0); the ending null character of string is included in the search. The strchr() function operates on null-ended strings.
You can compare string arrays and character vectors with relational operators and with the strcmp function. You can sort string arrays using the sort function, just as you would sort arrays of any other type. MATLAB® also provides functions to inspect characters in pieces of text.
With string arrays, you can use relational operators ( == , ~= , < , > , <= , >= ) instead of strcmp .
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) = '';
You can use strread:
str = '[email protected]';
[a b] = strread(str, '%s %s', 'delimiter','@')
a =
'johndoe'
b =
'hotmail.com'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With