如何获取字符串数组的字符串长度?

发布于 2024-10-03 13:45:52 字数 85 浏览 0 评论 0原文

我已经声明了一个字符串数组 String s;

我尝试通过给出 strlen(s) 来查找字符串的长度,但它不起作用。如何获取字符串的长度?

I have declared a string array String s;

I tried finding the length of the string by giving strlen(s) but it didnt work . How to get the length of the string ?

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

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

发布评论

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

评论(4

故事↓在人 2024-10-10 13:45:52

函数 strlen 不接受字符串,而是接受字符数组。请参阅此参考了解示例。如果您使用字符串类型,您可以使用函数 length() 找到它的长度,如下所示:

std::string text = "Some text"; 
int length = text.length(); 

有关更多示例,请参阅另一个问题

The function strlen does not take a string, but an array of chars. Please see this reference for examples. If you are using the type string you will find it's length by using the function length(), like this:

std::string text = "Some text"; 
int length = text.length(); 

For more examples see this other question.

听你说爱我 2024-10-10 13:45:52

使用成员函数length()。

Use the member function length().

请远离我 2024-10-10 13:45:52
string s;

声明一个名为 sstring,而不是字符串数组,例如

string array[10];

。要获取字符串的长度,请调用其 size()length() 方法:

size_t len = s.size();
string s;

declares a string named s, not an array of strings, which would be

string array[10];

for instance. To get the length of a string, you call its size() or length() method:

size_t len = s.size();
趁微风不噪 2024-10-10 13:45:52

我见过的很多采用 const char * (也可能是他所指的“字符串”)的方法将循环遍历变量,直到它们达到 NULL \0< /代码> 字符。

这通常是“字符串数组”的结束方式,并且通常是计算字符串长度的一个很好的停止点。

Alot of methods that I have seen take a const char * (which also might be the 'string' that he is refering to) will loop through the variable until they hit a NULL \0 character.

This often is how 'string arrays' are ended and will usually be a good stop to calculate the string length.

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