Converting a C-style string to a C++ std::string

99,705

Solution 1

C++ strings have a constructor that lets you construct a std::string directly from a C-style string:

const char* myStr = "This is a C string!";
std::string myCppString = myStr;

Or, alternatively:

std::string myCppString = "This is a C string!";

As @TrevorHickey notes in the comments, be careful to make sure that the pointer you're initializing the std::string with isn't a null pointer. If it is, the above code leads to undefined behavior. Then again, if you have a null pointer, one could argue that you don't even have a string at all. :-)

Solution 2

Check the different constructors of the string class: documentation You maybe interested in:

//string(char* s)
std::string str(cstring);

And:

//string(char* s, size_t n)
std::string str(cstring, len_str);

Solution 3

C++11: Overload a string literal operator

std::string operator ""_s(const char * str, std::size_t len) {
    return std::string(str, len);
}

auto s1 = "abc\0\0def";     // C style string
auto s2 = "abc\0\0def"_s;   // C++ style std::string

C++14: Use the operator from std::string_literals namespace

using namespace std::string_literals;

auto s3 = "abc\0\0def"s;    // is a std::string

Solution 4

If you mean char* to std::string, you can use the constructor.

char* a;
std::string s(a);

Or if the string s already exist, simply write this:

s=std::string(a);

Solution 5

You can initialise a std::string directly from a c-string:

std::string s = "i am a c string";
std::string t = std::string("i am one too");
Share:
99,705

Related videos on Youtube

Ian Burris
Author by

Ian Burris

Updated on July 09, 2022

Comments

  • Ian Burris
    Ian Burris almost 2 years

    What is the best way to convert a C-style string to a C++ std::string? In the past I've done it using stringstreams. Is there a better way?

    • Rob Kennedy
      Rob Kennedy over 13 years
      What's a cstring? Do you mean a CString from MFC? Or a null-terminated array of char (a C string)? Or something else?
  • Barney Szabolcs
    Barney Szabolcs over 8 years
    and now I also have to do delete myStr; no?
  • templatetypedef
    templatetypedef over 8 years
    @BarnabasSzabolcs No, that's not necessary. You only need to delete memory allocated with new. Pointers to string literals don't need to be deallocated.
  • Barney Szabolcs
    Barney Szabolcs over 8 years
    I see, so I'm guessing right, string makes an internal copy of this char *.
  • templatetypedef
    templatetypedef over 8 years
    @BarnabasSzabolcs Yes, though that's independent of whether you need to deallocate the original string.
  • Trevor Hickey
    Trevor Hickey over 8 years
    Every answer here fails to mention the obvious edge case. If your char* is NULL, std::string will throw. It will not be an empty string as many would suspect. It's unfortunate that all the top posts on stackoverflow don't mention this, and I suspect many people who google for this simple conversion are dealing with the bugs later.
  • Trevor Hickey
    Trevor Hickey over 8 years
    No. Your example would throw a logic error in std::string's constructor. 'a' cannot be NULL.
  • templatetypedef
    templatetypedef over 8 years
    @TrevorHickey While that's true, one could argue that NULL isn't a string. It's the absence of a string.
  • Trevor Hickey
    Trevor Hickey over 8 years
    @templatetypedef Agreed. The answers here aren't wrong, but a disclaimer about NULL would go a long way in terms of helping others. There are many common functions("getenv()" for example), that may or may not return NULL when called with the same inputs. By giving newcomers a simple one-liner without adding a disclaimer is setting them up for failure.
  • Bogatyr
    Bogatyr over 7 years
    @templatetypedef Yep, it was precisely a std::string s(getenv(foo)) throwing when the env didn't exist that led me here. It's sort of clunky that you can't initialize a string to a null value and have the string remain empty.
  • Vallerious
    Vallerious about 2 years
    a variable that is NULL or nullptr still has a type in statically-typed languages, so I do not think it is correct for std::string to throw or go into undefined behavior when given null. It should ideally initialise empty string.