本文目錄一覽:
C語言如何很好的解析字元串
解析字元串
可以靈活使用
memcmp strcmp strncmp等比較函數
另外 還有不區分大小的的比較函數,不同平台名字不同,非通用。
自己常用的一些比較 解析功能可以封裝為自定義函數,或者宏函數。
c語言解析字元串 ,大家請進
不知道你要解析的字元串是否都包含這些項目並且位置固定,所以寫了個通用的函數來取值。函數的3個參數分別是要解析的字元串,要取值的項目名,用來保存值的字元串,返回值表示是否成功。
#include stdio.h
#include string.h
int GetVal(const char *str, const char *name, char *val)
{
if(str = strstr(str, name))
for(str += strlen(name) + 1; *str *str != ‘;’; *val++ = *str++)
;
*val = ‘\0’;
return str != NULL;
}
int main()
{
char str[] = “Fee=500;MonthType=1;ChargeMode=1;IfAutoOrder=月份;Fee1=46;feeType=2;billingUnit=0;maxfee=0”;
char Fee[10], MonthType[10], ChargeMode[10], IfAutoOrder[10];
char Fee1[10], feeType[10], billingUnit[10], maxfee[10];
if(GetVal(str, “Fee”, Fee))
printf(“Fee=%s\n”, Fee);
if(GetVal(str, “MonthType”, MonthType))
printf(“MonthType=%s\n”, MonthType);
if(GetVal(str, “ChargeMode”, ChargeMode))
printf(“ChargeMode=%s\n”, ChargeMode);
if(GetVal(str, “IfAutoOrder”, IfAutoOrder))
printf(“IfAutoOrder=%s\n”, IfAutoOrder);
if(GetVal(str, “Fee1”, Fee1))
printf(“Fee1=%s\n”, Fee1);
if(GetVal(str, “feeType”, feeType))
printf(“feeType=%s\n”, feeType);
if(GetVal(str, “billingUnit”, billingUnit))
printf(“billingUnit=%s\n”, billingUnit);
if(GetVal(str, “maxfee”, maxfee))
printf(“maxfee=%s\n”, maxfee);
return 0;
}
c語言,解析字元串
Result:
ss[0]=home
ss[1]=ubuntu
ss[2]=test
ss[3]=homework
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/301868.html