std::string line = "";
int x = 7, y = 12;
What is the best way to concatenate the following and assign the result to the string variable, like so? (Obviously, this doesn't work.)
line = "(" + x + "," + y + ")";
std::string line = "";
int x = 7, y = 12;
line = "(" + x + "," + y + ")";
std::string s1 = "Hello";
std::string s2 = " World";
std::cout << s1.append(s2);
std::string s1 = "Hello";
std::string s2 = " World";
s1.append(s2);
std::cout << s1;
#include <sstream>
int main()
{
std::ostringstream builder;
builder << "Foo: " << (40 + 2) << " and such!";
std::cout << builder.str() << std::endl;
}
That gets me past dealing with trying to concatenate strings and char values. Now, could you throw a couple of ints into the mix?
std::stringstream stream;
stream << number;
return stream.str();
std::string line = "(%1,%2)";
int x = 7, y = 12;
line = String::Replace(line, "%1", IntToString(x));
line = String::Replace(line, "%2", IntToString(y));
//Replaces the _first_ occurance of 'toReplace' with 'replacement' in 'str'.
std::string Replace(const std::string &str, const std::string &toReplace, const std::string &replacement)
{
//Find the first occurance of 'toReplace'.
size_t pos = str.find(toReplace);
if(pos == std::string::npos)
return str;
//Create the new string as a copy of the old.
std::string newStr = str;
//Replace 'toReplace' in the new string with 'replacement'.
newStr.replace(pos, toReplace.size(), replacement);
return newStr;
}