日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言的字符串分割

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 */
  • 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更加合適,也更加啊靈活!

    推薦:
    http://www.cnblogs.com/roucheng/p/3454292.html

    轉載于:https://www.cnblogs.com/roucheng/p/cfenge.html

    總結

    以上是生活随笔為你收集整理的C语言的字符串分割的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。