C ++字符串长度? [英] C++ String Length?
本文介绍了C ++字符串长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取C ++中字符串中的字符数?
How should I get the number of characters in a string in C++?
推荐答案
c $ c> std :: string ,请致电 length()
:
If you're using a std::string
, call length()
:
std::string str = "hello";
std::cout << str << ":" << str.length();
// Outputs "hello:5"
如果使用c-字符串,请调用 strlen()
。
If you're using a c-string, call strlen()
.
const char *str = "hello";
std::cout << str << ":" << strlen(str);
// Outputs "hello:5"
或者,如果你碰巧喜欢使用Pascal样式字符串(或f *****字符串,如Joel Spolsky 喜欢呼叫他们当它们有一个尾部NULL),只是取消引用第一个字符。
Or, if you happen to like using Pascal-style strings (or f***** strings as Joel Spolsky likes to call them when they have a trailing NULL), just dereference the first character.
const char *str = "\005hello";
std::cout << str + 1 << ":" << *str;
// Outputs "hello:5"
这篇关于C ++字符串长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文