Jun 8, 2012 at 9:24pm
Does anybody know how I would go about converting a char array to a string?
Thanks.
Jun 8, 2012 at 9:27pm
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 Jun 8, 2012 at 9:28pm
Jun 8, 2012 at 10:18pm
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 Jun 8, 2012 at 10:18pm
Jun 9, 2012 at 3:10pm
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);