gpt4 book ai didi

c++ - C 字符串处理函数获取子字符串

转载 作者:行者123 更新时间:2023-11-30 20:46:04 24 4
gpt4 key购买 nike

我记得 C 或 C++ 中的一个函数可以执行以下操作:

string = "Hello my name is: 1234567 have a good day!"
function(string, "Hello my name is: %s have a good day!", substring)

子字符串将为 1234567 。那是什么function

最佳答案

在 C 语言中,您可以使用 sscanf() 来做到这一点。当然,C 中没有 string 数据类型:

#include <stdio.h>

int main(void)
{
const char string[] = "Hello my name is: 1234567 have a good day!";

char substring[32];
if(sscanf(string, "Hello my name is: %31s have a good day!", substring) == 1)
{
printf("got '%s'\n", substring);
}
return 0;
}

关于c++ - C 字符串处理函数获取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28171613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com