创建 C 字符串的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3036289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to create a C String
提问by Daniel
I'm currently using
我目前正在使用
char *thisvar = "stringcontenthere";
to declare a string in C.
在 C 中声明一个字符串。
Is this the best way to declare a string in C?
这是在 C 中声明字符串的最佳方法吗?
And how about generating a C-String from C++-Strings?
从 C++-Strings 生成 C-String 怎么样?
回答by David X
In C it depends on how you'll use the string:
在 C 中,这取决于您将如何使用字符串:
- named constant: your
char* str = "string";
method is ok (but should bechar const*
) - data to be passed to subfunction, but will not not used after the calling function returns:
char str[] = "string";
- data that will be used after the function it is declared in exits:
char* str = strdup("string");
, and make sure it getsfree
d eventually.
- 命名常量:你的
char* str = "string";
方法没问题(但应该是char const*
) - 要传递给子函数的数据,但在调用函数返回后将不再使用:
char str[] = "string";
- 在 exits: 中声明的函数之后将使用的数据
char* str = strdup("string");
,并确保它free
最终得到d 。
if this doesnt cover it, try adding more detail to your answer.
如果这没有涵盖它,请尝试在您的答案中添加更多细节。
回答by KM?n
const char *thisvar="stringcontenthere";
回答by ereOn
As other suggested, and I you want to "do it" the C++
way, use a std::string
.
正如其他人所建议的那样,我想以这种C++
方式“做到” ,请使用std::string
.
If you somehow need a C-string
, std::string
has a method that gives a const char*
.
如果你需要一个C-string
,std::string
有一个方法可以给出一个const char*
.
Here is an example:
下面是一个例子:
#include <iostream>
#include <string>
void dummyFunction(const char* str)
{
// Do something
}
int main(int, char**)
{
std::string str = "hello world!";
dummyFunction(str.c_str());
return EXIT_SUCCESS;
}
回答by math
It depends. For ASCII encoded strings see paragraphs C and C++. For unicode encoded strings see last paragraph.
这取决于。对于 ASCII 编码的字符串,请参见段落 C 和 C++。对于 unicode 编码的字符串,请参阅最后一段。
C:
C:
As David pointed out it depends on how to use the string in C:
正如大卫指出的那样,这取决于如何在 C 中使用字符串:
- as a constant then:
const char s[] = "Hello World";
- as a string containing variable data then:
char s[] = "Hello World";
- as a data array
char *data
; Initialization then should be customized.
- 作为一个常数:
const char s[] = "Hello World";
- 作为包含可变数据的字符串,则:
char s[] = "Hello World";
- 作为数据数组
char *data
;然后应该自定义初始化。
Please note in C there are all Strings Null-terminated, that means the definition of e.g. char s[] = "foo";
implicitly includes a NULL
character at the end s[3]='\0'
.
请注意,在 C 中,所有字符串都以 Null 结尾,这意味着 eg 的定义在末尾char s[] = "foo";
隐式包含一个NULL
字符s[3]='\0'
。
Also please note the subtile difference between char *s
and char s[]
which might often behave the same but sometimes not! (see Is an array name a pointer?) for example:
另请注意char *s
和之间的细微差别,char s[]
它们可能经常表现相同但有时不一样!(请参阅数组名称是指针吗?)例如:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[])
{
char s[] = "123456789123456789123456789";
char *t = (char*) malloc( sizeof(char) * 28 );
for( size_t i = 0; i < 28; ++i )
t[ i ] = 'j';
printf( "%lu\n", sizeof(s) );
printf( "%lu\n", sizeof(t) );
printf( "%s\n", s );
printf( "%s\n", t );
return EXIT_SUCCESS;
}
So I recommend to use char arrays whenever you use them as strings and char pointers whenever you use them as data array.
所以我建议在将它们用作字符串时使用 char 数组,而在将它们用作数据数组时使用 char 指针。
C++:
C++:
In C++ there is an own string data type: std::string
. If you just need to have a C-String version of a std::string (e.g. using some C-API) just use the c_str()
member:
在 C++ 中有一个自己的字符串数据类型:std::string
. 如果您只需要 std::string 的 C-String 版本(例如使用某些 C-API),只需使用c_str()
成员:
std::string s = "Hello World";
your_c_call( s.c_str(), ... );
Unicode:
统一码:
I you want to have unicode strings then you should really go with something like
我想要 unicode 字符串,那么你真的应该使用类似的东西
char utf8String[] = u8"Hello World";
and try not to use wchar_t
whenever possible. See this excellent article on that issue: http://www.nubaria.com/en/blog/?p=289. Please not that there is also unicode support for C++. But generally I am tempted to say that you should go with normal characters as far as you can. Interesting resource on that: http://www.cprogramming.com/tutorial/unicode.html
并尽量不要使用wchar_t
。请参阅有关该问题的这篇出色文章:http: //www.nubaria.com/en/blog/? p= 289。请注意,C++ 也有 unicode 支持。但总的来说,我很想说你应该尽可能地使用普通角色。有趣的资源:http: //www.cprogramming.com/tutorial/unicode.html
回答by Péter T?r?k
Is this C or C++? In C++ you should use std::string
:
这是 C 还是 C++?在 C++ 中,你应该使用std::string
:
std::string aString("stringcontenthere");