C语言的字符串分割
說起來很有意思,自認為對C語言理解得還是比較深刻的。但居然到今天才知道有個strtok函數,試用了一下突然感慨以前做了多少重復勞動。每次需要解析配置文件,每次需要分割字符串,居然都是自己去分割字符串,既累人又容易出錯。感概技術學得不夠全面啊!這里引用一段strtok用法:
The strtok() function returns a pointer to the next "token" in?str1, where?str2?contains the delimiters that determine the token. strtok() returns?NULL?if no token is found. In order to convert a string to tokens, the first call to strtok() should have?str1?point to the string to be tokenized. All calls after this should have?str1?be?NULL.
For example:
char str[] = "now # is the time for all # good men to come to the # aid of their country"; char delims[] = "#"; char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims ); } /* 何問起 hovertree.com */推薦:
http://www.cnblogs.com/roucheng/p/3454292.html
轉載于:https://www.cnblogs.com/roucheng/p/cfenge.html
總結
- 上一篇: sio2是什么化学元素
- 下一篇: swift编写命令行工具