【问题标题】:How to get word from string containing numbers through sscanf?如何通过sscanf从包含数字的字符串中获取单词?
【发布时间】:2022-06-27 23:27:52
【问题描述】:

我有字符串“word123”需要使用 sscanf 只提取“word”。

          char str[10];
          sscanf("word1233","%s", str);

【问题讨论】:

  • 您最好使用适当的解析器逐个字符地进行...但是...sscanf("word123", "%[^0123456789]", str);应该这样做...ideone.com/76L81O

标签: c scanf


【解决方案1】:

根据要求,我认为这不是最好的方法,但如果你想这样做,我会简单地接受一个忽略数字的字符串。

sscanf("word1233","%[^0123456789]", str);

您也可以只指定允许的字符:

sscanf("word1233","%[abcdefghijklmnopqrstuvwxyz]", str);

请注意,这只允许小写字母。

【讨论】:

    【解决方案2】:

    您可以使用%[^...] 仅匹配不在括号中的字符。然后你可以在那里列出数字字符。

    char str[10];
    sscanf("word1233", "%[^0123456789]", str);
    

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2023-03-24
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      相关资源
      最近更新 更多