从 Char* 数组(C 字符串)中提取数字

发布于 2024-12-12 01:03:11 字数 217 浏览 0 评论 0原文

我有一个以这种格式出现的字符串:

.word 40

我想提取整数部分。整数部分始终不同,但字符串始终以 .word 开头。我有一个标记生成器函数,它适用于除此之外的所有内容。当我将 .word (.word 带空格)作为分隔符时,它返回 null。

我怎样才能提取号码?

谢谢

I have a string that occurs in this format:

.word 40

I would like to extract the integer part. The integer part is always different but the string always starts with .word. I have a tokenizer function which works on everything except for this. When I put .word (.word with a space) as a delimiter it returns null.

How can I extract the number?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

初心未许 2024-12-19 01:03:11

您可以使用 strtok() 来提取以空格作为分隔符的两个字符串。

在线演示:

    #include <stdio.h>
    #include <string.h>

    int main ()
    {
        char str[] =".Word 40";
        char * pch;
        printf ("Splitting string \"%s\" into tokens:\n",str);
        pch = strtok (str," ");
        while (pch != NULL)
        {
            printf ("%s\n",pch);
            pch = strtok (NULL, " ");
        }
        return 0;
    }

输出:

Splitting string ".Word 40" into tokens:
.Word
40

如果您想要数字40 作为数值而不是字符串,那么您可以进一步使用
atoi() 将其转换为数值。

You can use strtok() to extract the two strings with space as an delimiter.

Online Demo:

    #include <stdio.h>
    #include <string.h>

    int main ()
    {
        char str[] =".Word 40";
        char * pch;
        printf ("Splitting string \"%s\" into tokens:\n",str);
        pch = strtok (str," ");
        while (pch != NULL)
        {
            printf ("%s\n",pch);
            pch = strtok (NULL, " ");
        }
        return 0;
    }

Output:

Splitting string ".Word 40" into tokens:
.Word
40

If you want the number 40 as a numeric value rather than a string then you can further use
atoi() to convert it to a numeric value.

酒浓于脸红 2024-12-19 01:03:11

您可以使用 sscanf 从字符串中提取格式化数据。 (它的工作方式与 scanf 类似,但从字符串而不是标准输入中读取数据)

You can use sscanf to extract formated data from a string. (It works just like scanf, but reading the data from a string instead of from standard input)

老子叫无熙 2024-12-19 01:03:11

检查字符串

strncmp(".word ", (your string), 6);

如果返回 0,那么您的字符串以“.word”开头,然后您可以查看(您的字符串)+ 6 以找到数字的开头。

Check the string with

strncmp(".word ", (your string), 6);

If this returns 0, then your string starts with ".word " and you can then look at (your string) + 6 to get to the start of the number.

成熟的代价 2024-12-19 01:03:11
char str[] = "A=17280, B=-5120. Summa(12150) > 0";
char *p = str;
do
{
if (isdigit(*p) || *p == "-" && isdigit(*(p+1)))
printf("%ld ", strtol(p,&p,0);
else
p++;
}while(*p!= '\0');

此代码在控制台中写入所有数字。

char str[] = "A=17280, B=-5120. Summa(12150) > 0";
char *p = str;
do
{
if (isdigit(*p) || *p == "-" && isdigit(*(p+1)))
printf("%ld ", strtol(p,&p,0);
else
p++;
}while(*p!= '\0');

This code write in console all digits.

过期情话 2024-12-19 01:03:11
int foo;
scanf("%*s %d", &foo);

星号告诉 scanf 不要存储它读取的字符串。如果要从文件中读取,请使用 fscanf;如果输入已在缓冲区中,请使用 sscanf。

int foo;
scanf("%*s %d", &foo);

The asterisk tells scanf not to store the string it reads. Use fscanf if you're reading from a file, or sscanf if the input is already in a buffer.

爱格式化 2024-12-19 01:03:11

又快又脏:

char* string = ".word 40";
char number[5];
unsigned int length = strlen(string);
strcpy(number, string + length - 2);

Quick and dirty:

char* string = ".word 40";
char number[5];
unsigned int length = strlen(string);
strcpy(number, string + length - 2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文