Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a specific character in a string in Matlab

Tags:

string

matlab

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?

like image 724
stanigator Avatar asked Sep 15 '09 04:09

stanigator


People also ask

How do I find a character in a 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.

How do I compare part of a string in MATLAB?

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.

Can you use == for strings in MATLAB?

With string arrays, you can use relational operators ( == , ~= , < , > , <= , >= ) instead of strcmp .


2 Answers

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) = '';
like image 91
gnovice Avatar answered Sep 19 '22 06:09

gnovice


You can use strread:

str = '[email protected]';
[a b] = strread(str, '%s %s', 'delimiter','@')
a = 
    'johndoe'
b = 
    'hotmail.com'
like image 42
Amro Avatar answered Sep 20 '22 06:09

Amro