views:

3653

answers:

8
+2  Q: 

C++ String Length?

How should I get the number of characters in a string in C++?

+1  A: 
string foo;
... foo.length() ...

.length and .size are synonymous, I just think that "length" is a slightly clearer word.

Alex Martelli
size() is universal across containers, though.
rlbond
size() is discouraged because I remember hearing that in some implementations it will return the "length() + 1" (because it includes the null terminator). So just use length() as it is cleaner and more portable.
nlaq
@LaQuet, If you're not using a standards-compliant compiler or STL implementation you have more things to worry about.
strager
@Nelson that shouldn't be the case as by standard C++ Strings aren't null-terminated. However when casting with c_str() it will add the null byte to the end.
John T
+1  A: 
std::string str("a string");
std::cout << str.size() << std::endl;
stefanB
+25  A: 

If you're using a std::string, call length():

std::string str = "hello";
cout << str << ":" str.length();
// Outputs "hello:5"

If you're using a c-string, call strlen().

const char *str = "hello";
cout << str << ":" << strlen(str);
// Outputs "hello:5"

Or, if you happen to like using Pascal-style strings (or f***ed strings as Joel Spolsky likes to call them when they have a trailing NULL), just dereference the first character.

const char *str = "\005hello";
cout << str + 1 << ":" << *str;
// Outputs "hello:5"
Eclipse
+4  A: 

When dealing with C++ strings (std::string), you're looking for length() or size(). Both should provide you with the same value. However when dealing with C-Style strings, you would use strlen().

#include <iostream>
#include <string.h>

int main(int argc, char **argv)
{
   std::string str = "Hello!";
   const char *otherstr = "Hello!"; // C-Style string
   std::cout << str.size() << std::endl;
   std::cout << str.length() << std::endl;
   std::cout << strlen(otherstr) << std::endl; // C way for string length
   std::cout << strlen(str.c_str()) << std::endl; // convert C++ string to C-string then call strlen
   return 0;
}

Output:

6
6
6
6
John T
Note that you can avoid compiler warnings (in certain C++ compilers) by using #include <cstring> instead of the deprecated#include <string.h>
Colin
+1  A: 

for an actual string object:

yourstring.length();

or

yourstring.size();
Luke Schafer
+1  A: 

If you're using old, C-style string instead of the newer, STL-style strings, there's the strlen function in the C run time library:

const char* p = "Hello";
size_t n = strlen(p);
ChrisW
+3  A: 

It depends on what string type you're talking about. There are many types of strings:

  1. const char* - a C-style multibyte string
  2. const wchar_t* - a C-style wide string
  3. std::string - a "standard" multibyte string
  4. std::wstring - a "standard" wide string

For 3 and 4, you can use .size() or .length() methods.

For 1, you can use strlen(), but you must ensure that the string variable is not NULL (=== 0)

For 2, you can use wcslen(), but you must ensure that the string variable is not NULL (=== 0)

There are other string types in non-standard C++ libraries, such as MFC's CString, ATL's CComBSTR, ACE's ACE_CString, and so on, with methods such as .GetLength(), and so on. I can't remember the specifics of them all right off the top of my head.

The STLSoft libraries have abstracted this all out with what they call string access shims, which can be used to get the string length (and other aspects) from any type. So for all of the above (including the non-standard library ones) using the same function stlsoft::c_str_len(). This article describes how it all works, as it's not all entirely obvious or easy.

dcw
+1  A: 

if you're using std::string, there are two common methods for that:

std::string Str("Some String");
size_t Size = 0;
Size = Str.size();
Size = Str.length();

if you're using the C style string (using char * or const char *) then you can use:

const char *pStr = "Some String";
size_t Size = strlen(pStr);
Gal Goldman