开发者

How to take string length of a String array?

开发者 https://www.devze.com 2023-01-26 12:38 出处:网络
I have declared a st开发者_JS百科ring 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 ?The function strlen does no

I have declared a st开发者_JS百科ring 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 ?


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.


Use the member function length().


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();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号