How to get the number of characters in a std::string?

前端 未结 12 1015
既然无缘
既然无缘 2020-11-28 03:52

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

12条回答
  •  不知归路
    2020-11-28 04:38

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

提交回复
热议问题