我有char数组,包含这样的信息(基本上是字符串):
char arr[] = "Age: 22 Height: 180 Weight: 80";
我想从这个Char数组中获取具体信息.例如,我需要高度数(在这种情况下为180)并将其存储在另一个Char数组中.
我可以通过使用来达到"高度:180威......".
strstr(arr,"Height: ");
但我需要摆脱结束,只有简单的"180"数字
我知道我应该使用子字符串或其他类似的功能来分割字符串.但我只是不想象如何解决这个问题.有任何想法吗 ?谢谢
如果您需要这些数字,请使用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