连接字符串的C / C ++ [英] Concatenate Strings on C/C++

查看:90
本文介绍了连接字符串的C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何连接字符串使用C / C ++?

How do I concatenate Strings with C/C++?

我尝试以下方式:

PS:errorInfo中是一个char *我应该回到它

PS: errorInfo is a char * I should return it.

        errorInfo = strcat("Workflow: ", strcat(
            workflowToString(workflow).utf8(), strcat(" ERROR: ",
                    errorCode.utf8)));

        sprintf(errorInfo, "Workflow %s ERROR: %s",
            workflowToString(workflow).utf8(), errorCode.utf8());

        errorInfo = "Workflow: " + workflowToString(workflow).utf8() + " ERROR: " + errorCode.utf8;

就在sprintf的编译,但运行我的应用程序崩溃的时候。

Just the sprintf compiles but when running my application crash.

PS:我使用NDK Android电子

PS: I'm using NDK from Android

推荐答案

根据这页面 strcat的执行以下操作:

将源字符串到目的字符串的副本。在目标的终止空字符由源的第一个字符覆盖,和一个新的空字符被附加在双方的目的地串联形成的新字符串的结尾。

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.

在您的实现,然而,工作流程:是一个常量字符串。您不能修改该字符串,这是什么 strcat的会做。为了做到这一点,创建一个字符串,如:

In your implementation, however, "Workflow: " is a constant string. You cannot modify that string, which is what strcat would do. In order to do that, create a string like:

char message[1000];
strcpy(message, "Workflow: ");
strcat(message, "other string");
....

不过,要小心的UTF8字符编码,因为一个UTF8 code点可能有多个字符取值长。

这篇关于连接字符串的C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆