将整数粘贴到字符串和字符*

发布于 2024-08-17 05:44:52 字数 170 浏览 3 评论 0原文

如何将整型变量添加到字符串和 char* 变量中?例如:
int a = 5;
字符串 St1 = "书", St2;
char *Ch1 =“注释”,Ch2;

St2 = St1 + a -->第五册
Ch2 = Ch1 + a -->注5

谢谢

How can I add an integer variable to a string and char* variable? for example:
int a = 5;
string St1 = "Book", St2;
char *Ch1 = "Note", Ch2;

St2 = St1 + a --> Book5
Ch2 = Ch1 + a --> Note5

Thanks

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

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

发布评论

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

评论(4

过气美图社 2024-08-24 05:44:53

C++ 执行此操作的方法是:

std::stringstream temp;
temp << St1 << a;
std::string St2 = temp.str();

您也可以使用 Ch1 执行相同的操作:

std::stringstream temp;
temp << Ch1 << a;
char* Ch2 = new char[temp.str().length() + 1];
strcpy(Ch2, temp.str().c_str());

The C++ way of doing this is:

std::stringstream temp;
temp << St1 << a;
std::string St2 = temp.str();

You can also do the same thing with Ch1:

std::stringstream temp;
temp << Ch1 << a;
char* Ch2 = new char[temp.str().length() + 1];
strcpy(Ch2, temp.str().c_str());
慕巷 2024-08-24 05:44:53

例如,对于 char* ,您需要创建另一个足够长的变量。您可以“修复”输出字符串的长度,以消除超出字符串末尾的可能性。如果这样做,请小心使其足够大以容纳整个数字,否则您可能会发现 book+50 和 book+502 都显示为 book+50 (截断)。

以下是如何手动计算所需的内存量。这是最有效但容易出错的方法。

int a = 5;
char* ch1 = "Book";
int intVarSize = 11; // assumes 32-bit integer, in decimal, with possible leading -
int newStringLen = strlen(ch1) + intVarSize + 1; // 1 for the null terminator
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
snprintf(ch2, intVarSize, "%s%i", ch1, a);

ch2 现在包含组合文本。

或者,稍微不那么棘手,也更漂亮(但效率较低),您还可以对 printf 进行“试运行”以获得所需的长度:

int a = 5;
char* ch1 = "Book";
// do a trial run of snprintf with max length set to zero - this returns the number of bytes printed, but does not include the one byte null terminator (so add 1)
int newStringLen = 1 + snprintf(0, 0, "%s%i", ch1, a);
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; } 
// do the actual printf with real parameters.
snprintf(ch2, newStringLen, "%s%i", ch1, a);

如果您的平台包含 asprintf,那么这会容易得多,因为 asprintf 会自动分配正确的长度新字符串的内存量。

int a = 5;
char* ch1 = "Book";
char* ch2;
asprintf(ch2, "%s%i", ch1, a);

ch2 现在包含组合文本。

c++ 没有那么繁琐,但我将把它留给其他人来描述。

for char* you need to create another variable that is long enough for both, for instance. You can 'fix' the length of the output string to remove the chance of overrunning the end of the string. If you do that, be careful to make this large enough to hold the whole number, otherwise you might find that book+50 and book+502 both come out as book+50 (truncation).

Here's how to manually calculate the amount of memory required. This is most efficient but error-prone.

int a = 5;
char* ch1 = "Book";
int intVarSize = 11; // assumes 32-bit integer, in decimal, with possible leading -
int newStringLen = strlen(ch1) + intVarSize + 1; // 1 for the null terminator
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
snprintf(ch2, intVarSize, "%s%i", ch1, a);

ch2 now contains the combined text.

Alternatively, and slightly less tricky and also prettier (but less efficient) you can also do a 'trial run' of printf to get the required length:

int a = 5;
char* ch1 = "Book";
// do a trial run of snprintf with max length set to zero - this returns the number of bytes printed, but does not include the one byte null terminator (so add 1)
int newStringLen = 1 + snprintf(0, 0, "%s%i", ch1, a);
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; } 
// do the actual printf with real parameters.
snprintf(ch2, newStringLen, "%s%i", ch1, a);

if your platform includes asprintf, then this is a lot easier, since asprintf automatically allocates the correct amount of memory for your new string.

int a = 5;
char* ch1 = "Book";
char* ch2;
asprintf(ch2, "%s%i", ch1, a);

ch2 now contains the combined text.

c++ is much less fiddly, but I'll leave that to others to describe.

归途 2024-08-24 05:44:53

您需要创建另一个足够大的字符串来容纳原始字符串,后跟数字(即将与数字的每个数字对应的字符附加到这个新字符串)。

You need to create another string large enough to hold the original string followed by the number (i.e. append the character corresponding to each digit of the number to this new string).

伏妖词 2024-08-24 05:44:53
Try this out:
char *tmp  = new char [ stelen(original) ];
itoa(integer,intString,10);

output = strcat(tmp,intString);

//use output string 

delete [] tmp;
Try this out:
char *tmp  = new char [ stelen(original) ];
itoa(integer,intString,10);

output = strcat(tmp,intString);

//use output string 

delete [] tmp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文