【问题标题】:Get phone number as string from long string in c从c中的长字符串中获取电话号码作为字符串
【发布时间】:2016-07-15 16:29:45
【问题描述】:

我有 Telit 调制解调器,我想获取 sim 电话号码,Telit 返回字符串如下:

"+CNUM: "","+123456789123456",**145**ok"

Or : "+CNUM: "","+123456789123456",**129**ok"

只有数字145(International), 129(National)不同。

我只想得到数字:+123456789123456,没有“+CNUM:””,也没有,129。

我试过了:

responseBuffer ="+CNUM: "",""+123456789123456"",145";
sscanf(responseBuffer,"%*s %s",phoneNum);   // cut the beginning

我如何摆脱字符串的重置,从',' 到结尾的字符?

【问题讨论】:

  • 您能否尝试正确格式化输入字符串(您让responseBuffer 指向的字符串),因为这样会导致编译器错误。
  • 你实际尝试过什么?
  • 那么"+CNUM: ,+467190002574211,145" 真的吗?
  • 你试过strtok吗?
  • @user5742600 如果您需要在字符串中包含引号,请使用转义字符!在这种情况下会有这样的东西:"+CNUM: \"\",\"+123456789123456\",145ok"

标签: c substring


【解决方案1】:

感谢@JoachimPileborg 我使用了strtok

responseBuffer ="+CNUM: "",""+123456789123456"",145";
token = strtok(responseBuffer, ",");
if( token != NULL ) 
  token = strtok(NULL, ",");
strcpy(phoneNum,token);

【讨论】:

    【解决方案2】:

    您可以尝试搜索“+”字符的代码,找到后将电话号码存储到字符串指针中。

    storePhoneNumber(const char* input, char* dest){
    //loop over the input
    //look for '+' char
    //when found start to store numbers on by one
    // put it in to the dest
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 2023-03-16
      • 2013-10-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多