Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a substring in C

Tags:

c

linux

i am trying to extract the username from this uri field in ANSI C code on linux using gcc

mail:[email protected]

so i need to strip the mail: and everything after the @. Are there any built in functions in C to extract substrings

like image 245
tech74 Avatar asked Aug 13 '10 16:08

tech74


1 Answers

char *uri_field = "mail:[email protected]";

char username[64];

sscanf(uri_field, "mail:%63[^@]", username);

If you might have other "junk" at the beginning (not necessarily just mail:), you could do something like this instead:

sscanf(uri_field, "%*[^:]:%63[^@]", username);
like image 74
Jerry Coffin Avatar answered Oct 05 '22 22:10

Jerry Coffin