使用sscanf來提取字符串中的指定內容, 感受還不錯, 如今在這裏作一個總結android
注意代碼中變量均無定義, 能夠根據用法自行定義.ios
//1.基本用法 string = "hello world 88888"; sscanf(string, "%s %s %d", buf1, buf2, &digit); //sscanf返回提取變量的個數
//2.提取指定長度的字符串 string = "a123ddd444"; sscanf(string, "%5s", buf1); printf("%s\n", buf1); //a123d
//3.提取到指定字符爲止的字符串 string = "Content-Length: 2556"; sscanf(string, "%[^:]:%d", buf1, &digit); /*buf1:Content-Length digit: 2556*/
//3.提取到指定字符集 string = "123abcABC"; sscanf(string, "%[^a-z]", buf1); /*buf1: 123*/
//4.獲取到指定字符中間的字符 string = "ios<android>wp7"; sscanf(string, "%*[^<]<%[^>]", buf1); /*buf1: android*/
//5.指定有跳過的字符 string = "iosVSandroid"; sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2); /*buf1: ios buf2: android*/
//6.分割以某字符爲間隔的字符串 string = "android-ios-wp7"; sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);
//7.過濾掉不要的字符串 //用在%號後面加一*號,表明過濾這個字符串,不讀取 string = "android ios wp7"; sscanf(string, "%s %*s %*s", buf1);