|
|
Test case 1:
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str[] ="This is a sample string, just testing.";
- char * pch;
- printf ("Splitting string "%s" in tokens:\n",str);
- pch = strtok (str,","); //精确定义一个截断符","
- while (pch != NULL)
- {
- printf ("%s\n",pch);
- pch = strtok (NULL, " ");
- }
- return 0;
- }
复制代码
Result :
- [root@root ANSI]# ./a.out
- Splitting string "This is a sample string, just testing." in tokens:
- This is a sample string
- just
- testing.
- [root@root ANSI]#
复制代码
Test case 2:
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str[] ="This is a sample string, just testing.";
- char * pch;
- printf ("Splitting string "%s" in tokens:\n",str);
- pch = strtok (str," ,"); //截断符","前面加上一个空格
- while (pch != NULL)
- {
- printf ("%s\n",pch);
- pch = strtok (NULL, " ");
- }
- return 0;
- }
复制代码
Result:
- [root@root ANSI]# ./a.out
- Splitting string "This is a sample string, just testing." in tokens:
- This
- is
- a
- sample
- string,
- just
- testing.
- [root@root ANSI]#
复制代码
例2中为什么没有用","作为截断符呢? |
|