提取在C子 [英] Extract a substring in C

查看:154
本文介绍了提取在C子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用gcc在linux上的ANSI C code这个URI字段中提取用户名

 信箱:username@example.com

所以我需要剥离邮箱:@后的一切。是否有任何内置函数在C子提取


解决方案

 的char * uri_field =邮箱:username@example.com字符的用户名[64];sscanf的(uri_field,邮件:%63 [^ @],用户名);

如果你可能有其他的垃圾开头(不一定只是邮箱:),你可以做这样的事情,而不是:

 的sscanf(uri_field,%* [^:]:%63 [^ @],用户名);

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

mail:username@example.com

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

解决方案

char *uri_field = "mail:username@example.com";

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);

这篇关于提取在C子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆