C,从字符串中获取需要的元素

时间:2023-01-21 15:15:14

I have char array that contains information like so (which is basically string) :

我有char数组,包含了这样的信息(基本上是字符串):

char arr[] = "Age: 22 Height: 180 Weight: 80";

I want to get specific information from this Char array. For example i need Height number (which would be 180 in this case) and store it in another Char array.

我想从这个Char数组中获取特定的信息。例如,我需要高度号(在本例中为180)并将它存储在另一个Char数组中。

I can get to "Height: 180 Wei...." by using.

我可以通过使用“高度:180魏....”。

strstr(arr,"Height: ");

but i need to get rid of the end and have just plain "180" number

但是我需要去掉末端,只剩下普通的“180”。

I know i should be using sub string or other similar functions that split the string. but i just don't imagine how to solve this. Any ideas ? Thank you

我知道我应该使用子字符串或者其他类似的函数来分割字符串。但是我想不出怎么解决这个问题。什么好主意吗?谢谢你!

4 个解决方案

#1


5  

If you need the numbers, use sscanf():

如果需要这些数字,请使用sscanf():

int age, height, weight;
const int result = sscanf(arr, "Age: %d Height: %d Weight: %d", &age, &height, &weight);

if (result < 3) // error: couldn't match all elements

#2


0  

char *height_with_label = strstr(arr,"Height: ");
size_t label_len = strlen("Height: ");
int height = default_value;
if (height_with_label)
{
    height = atoi(height_with_label + label_len);
}

#3


0  

Initialize an integer h to 0.

将整数h初始化为0。

Iterate through the characters following "Height ". Test whether the character is a digit. If it's a digit, multiply h by 10 and add that digit, else end the iteration.

遍历“Height”后面的字符。测试字符是否为数字。如果是一个数字,将h乘以10,然后相加,否则就结束了迭代。

No functions necessary. If you want to use a function to make your life easier, read a reference guide on the C standard library, such as the one on The GNU C Library.

没有必要的功能。如果您想使用一个函数来使您的生活更轻松,请阅读C标准库的参考指南,例如GNU C库中的参考指南。

#4


0  

Use a pointer to point to the end of the "height:" word like this:

使用一个指针指向“height:”这个词的结尾:

char *p = strstr(arr, "height:") + strlen("height:");

Then use the atof function in string library to get the rest of the array as a float. It will stops at the first undefined char (a non numerical character) and it will gets you the value of the height.

然后使用字符串库中的atof函数将数组的其余部分作为浮点数。它将在第一个未定义的char(一个非数字字符)中停止,它将使您得到高度的值。

float height = atof(p);

You can use atoi to get an integer instead of a float or atol to get a long integer.

您可以使用atoi获得一个整数,而不是浮点数或atol来获得一个长整数。

Make sure to include the stdlib.h library to use these functions.

确保包含stdlib。h库使用这些函数。

The same think for the other numerical informations.

其他数值信息也是一样。

#1


5  

If you need the numbers, use sscanf():

如果需要这些数字,请使用sscanf():

int age, height, weight;
const int result = sscanf(arr, "Age: %d Height: %d Weight: %d", &age, &height, &weight);

if (result < 3) // error: couldn't match all elements

#2


0  

char *height_with_label = strstr(arr,"Height: ");
size_t label_len = strlen("Height: ");
int height = default_value;
if (height_with_label)
{
    height = atoi(height_with_label + label_len);
}

#3


0  

Initialize an integer h to 0.

将整数h初始化为0。

Iterate through the characters following "Height ". Test whether the character is a digit. If it's a digit, multiply h by 10 and add that digit, else end the iteration.

遍历“Height”后面的字符。测试字符是否为数字。如果是一个数字,将h乘以10,然后相加,否则就结束了迭代。

No functions necessary. If you want to use a function to make your life easier, read a reference guide on the C standard library, such as the one on The GNU C Library.

没有必要的功能。如果您想使用一个函数来使您的生活更轻松,请阅读C标准库的参考指南,例如GNU C库中的参考指南。

#4


0  

Use a pointer to point to the end of the "height:" word like this:

使用一个指针指向“height:”这个词的结尾:

char *p = strstr(arr, "height:") + strlen("height:");

Then use the atof function in string library to get the rest of the array as a float. It will stops at the first undefined char (a non numerical character) and it will gets you the value of the height.

然后使用字符串库中的atof函数将数组的其余部分作为浮点数。它将在第一个未定义的char(一个非数字字符)中停止,它将使您得到高度的值。

float height = atof(p);

You can use atoi to get an integer instead of a float or atol to get a long integer.

您可以使用atoi获得一个整数,而不是浮点数或atol来获得一个长整数。

Make sure to include the stdlib.h library to use these functions.

确保包含stdlib。h库使用这些函数。

The same think for the other numerical informations.

其他数值信息也是一样。