国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

淺談C語言的字符串分割

2020-01-26 14:34:59
字體:
來源:轉載
供稿:網友

說起來很有意思,自認為對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 */

The above code will display the following output: 

result is "now " 

result is " is the time for all " 

result is " good men to come to the " 

result is " aid of their country" 

這個函數跟編譯器中的詞法分析很像,在以后的文本處理中,會解決很多問題。看來我有必要系統的學習下C的庫函數,而不僅僅是死扎在語法和一些算法技巧上面。這樣在平常的工作中才能事半功倍。

使用這個函數,形如下面的配置文件就非常容易解析:

id1 value1 value2 value3

id2 value1 value2 value3

...

使用這個函數,分割字符串就更加方便了,例如下面待分割的字符串:

12|2345|asld|alsfalskd

只要讀取待處理的數據,然后調用四次strtok就能夠解析出每行的值,以前我一般不是自己解析就是用sscanf,但是strtok更加合適,也更加啊靈活!

以上這篇淺談C語言的字符串分割就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 兴城市| 睢宁县| 济南市| 繁昌县| 长沙县| 虹口区| 高雄县| 长宁区| 雅安市| 石河子市| 江川县| 凤庆县| 晴隆县| 马关县| 当阳市| 肇源县| 广平县| 绵竹市| 松江区| 鱼台县| 嘉兴市| 道孚县| 宜阳县| 滨海县| 望城县| 洪湖市| 曲沃县| 五大连池市| 安远县| 铅山县| 高邮市| 洛川县| 阿克苏市| 雅安市| 唐山市| 沅江市| 伊川县| 原阳县| 原阳县| 潜山县| 扶沟县|