user6245072
user6245072

Reputation: 2161

How can I append part of an array of characters to a string?

Suppose I have a std::string object and a null-terminated array of characters (or C-style string):

std::string str("This is a ");

const char* cstr = "strings are a really important data type.";

How can I append just the first N characters (in this case, 6, so that str will contain This is a string) of a C-style string to a std::string in the cleanest and most efficient way possible?

Upvotes: 1

Views: 965

Answers (2)

Raja Sudhan
Raja Sudhan

Reputation: 121

You can convert const char* to string and then add it for concatenation-

std::string a("Hello ");  
const char* b="World!!!";  
std::string a1(b,5);  
a=a+a1;  

Upvotes: 0

user2146414
user2146414

Reputation: 1038

How about the append method?

str.append(cstr, 6);

Upvotes: 2

Related Questions