公司代码喜欢sscanf,sprintf等函数,字符串比对确实有大用处,记录下
#include
int main()
{
char buf[512] = "";
sscanf("123456","%s",buf);
printf("buf = %s\n",buf); //123456
sscanf("123456","%4s",buf);
printf("buf = %s\n",buf);//1234
sscanf("123456abds","%[^d]",buf);
printf("buf = %s\n",buf);//123456ab 取到d为止
sscanf("123456abcBGFDd","%[1-9a-z]",buf);
printf("buf = %s\n",buf);//123456abc 遇到不符合1-9,a-z条件直接退出
sscanf("ios/4324@345","%*[^/]/%[^@]",buf);
printf("buf = %s\n",buf);//*代表忽略第一个到/直接的字符,也就是ios,然后取到@之间的就是4324
sscanf("hello world","%*s%s",buf);
printf("buf = %s\n",buf);//忽略hello,遇到空格停止,所以结果是world.
sscanf("hello _world","%*s _%s",buf);
printf("buf = %s\n",buf);//world,注意%s 与_%s中间有空格
//注意:以下这种情况,不匹配会直接后面全部忽略
char buf2[10]="";
char buf3[10]="";
sscanf("Cmd=1223|Job=43|name=111","Cmd=%[^|]|ip=%[^|]|name=%[^|]|",buf,buf2,buf3);
printf("buf = %s,buf2=%s,buf3=%s\n",buf,buf2,buf3);
sscanf("Cmd=1223|Job=43|name=111","Cmd=%[^|]|Job=%[^|]|name=%[^|]|",buf,buf2,buf3);
printf("buf = %s,buf2=%s,buf3=%s\n",buf,buf2,buf3);
sscanf("Cmd=1223|Job=43|name=111","Cmd=%[^|]|Job=%*[^|]|name=%[^|]|",buf,buf2);
printf("buf = %s,buf2=%s\n",buf,buf2);
}