Convert Char array to String

Does anybody know how I would go about converting a char array to a string?

Thanks.
where someCharArray is an array of chars (or a pointer to the first element in such an array)

string aNiceString(someCharArray);

will give you a string object named aNiceString
Last edited on
You can do it by declaration of a string or by assignment operator. For example

1
2
3
std::string s = "This is a declaration";

s = "This is  an assignment";
Last edited on
If the char array is not null terminated the above will not work. In that case you have to specify the length to the string constructor.

string aNiceString(someCharArray, lengthOfCharArray);
Topic archived. No new replies allowed.