//一、格式化输出字符串,相当于strcpy
sscanf("123456", "%s", str);
/*output 123456*/
//二、取指定长度的字符串
sscanf("12345678", "%4s", str);
/*output 1234*/
//三、取到指定字符串为止。
sscanf("123456789 abcdfe", "%[^ ]", str);
/*output 123456789*/
//四、取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("89hkDFCCQQQQ", "%[1-9a-z]", str); //123SFDAF89DFCC9QQQQsssssfdfa
/*output 89hk*/
//五、取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
sscanf("123456789fdsafdsaDDDDDD", "%[^A-Z]", str);
/*output 123456789fdsafdsa*/
//六、取出下面字符串中数字2位为一组取出
char a1[3], a2[3], a3[3]; int i1,i2;
sscanf("12:34:56-7890", "%2s:%2s:%2s-%2d%2d", a1, a2, a3, &i1, &i2);
printf("--%s---%s----%s----%d----%d---\n", a1, a2, a3, i1, i2);
/*output 12 34 56 78 90*/
//七、以','为分割取出下面字符串
char a4[10], a5[10], a6[10], a7[10];
sscanf("first,25.5,second,15", "%5s,%4s,%6s,%2s", a4, a5, a6, a7);
/*method one*/
sscanf("first,25.5,second,15", "%[^,],%[^,],%[^,],%s", a4, a5, a6, a7);/*method two*/
printf("--%s---%lf----%s----%d--\n", a4, atof(a5), a6, atoi(a7));
/*output first 25.500000 seond 15*/
//八、给定一个字符串iios/12DDWDFF@122,获取 '/' 和 '@' 之间的字符串,
//先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中
sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", str);
/*ouput: 12DDWDFF*/
//九、给定一个字符串"hello, world",仅保留world。
//(注意:","之后有一空格,%s遇空格停止,加*则是忽略第一个读到的字符串)
sscanf("hello, world", "%*s%s", str);
/*output: wrold*/
//十、处理时间分割函数
char a8[15], a9[15];
//sscanf("2006:03:18 - 2006:04:18", "%s - %s", a8, a9);
//sscanf("2006:03:18-2006:04:18", "%s-%s", a8, a9);
//error
sscanf("2006:03:18-2006:04:18", "%[^-]-%s", a8, a9);
//method one
sscanf("2006:03:18-2006:04:18", "%10s-%10s", a8, a9);
//method two
printf("#######%s#########%s######\n", a8, a9);
/*output: 2006:03:18 2006:04:18*/
//十一、指定要跳过的字符串
char a10[15], a11[15];
sscanf("iosVSandroid", "%[a-z]VS%[a-z]", a10, a11);
printf("###%s######%s###\n", a10, a11);
/*output: ios android*/
//十二、提取邮箱地址
char a12[10], a13[10], a14[10], a15[10];
sscanf("Email:[email protected]", "%[^:]:%[^@]@%[^.].%s", a12, a13, a14, a15);
printf("#####%s#######%s#######%s######%s####\n", a12, a13, a14, a15);
/*output: Email beijing sina com.cn*/
return 0;
}