简体   繁体   English

如何在c / c ++中将一个int添加到char []

[英]how to add a int to a char[] in c/c++

i have a code like this: 我有这样的代码:

int i = 123;
char myString[100];
strcpy(myString, "my text");

and how i want to add the 123 after "my text". 以及我如何在“我的文字”之后添加123。 how to do this in c/c++? 如何在c / c ++中执行此操作?

at the end myString sould be my test123 最后myString应该是my test123

In C++: 在C ++中:

std::stringstream ss;
ss << "my text" << i;
std::string resultingString = ss.str();

There's no such thing as C/C++ . 没有C / C ++这样的东西

在C ++ 11中:

std::string result = "my text" + std::to_string(i);

(To complement Luchian's answer) (补充Luchian的答案)

In C: 在C:

char myString[128];
int i = 123;
snprintf(myString, sizeof(myString), "My Text %d", i);

假设mystring足够大,在这个例子中就是这样:

sprintf( myString, "my text%d", 123 );

使用C ++中的Qt QString对象:

std::cout << QString("my text %1").arg(123456);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM