Jump to content
  • Advertisement
Sign in to follow this  
Quasimojo

C++ string concatenation

This topic is 2466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, this may be a dumb question, but I've been banging my head on this one for some time now. Considering the following:

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 + ")";

Share this post


Link to post
Share on other sites
Advertisement
I recommend append.


std::string s1 = "Hello";
std::string s2 = " World";
std::cout << s1.append(s2);


I also like StringStream a lot when appending things like integers and doubles.

P.S. http://www.cplusplus.com is your friend.

P.S.S. std::string::append will append the parameter string to the current string and return the current string so


std::string s1 = "Hello";
std::string s2 = " World";
s1.append(s2);
std::cout << s1;


would display the same thing.

Share this post


Link to post
Share on other sites
That gets me past dealing with trying to concatenate strings and char values. Now, could you throw a couple of ints into the mix?

Share this post


Link to post
Share on other sites
Either use a stringstream or you can use boost::lexical_cast to easily convert the ints to strings and then concatenate with +

Share this post


Link to post
Share on other sites
It's all about the stringstream, baby:

#include <sstream>

int main()
{
std::ostringstream builder;
builder << "Foo: " << (40 + 2) << " and such!";

std::cout << builder.str() << std::endl;
}



Note that the syntax for cout and stringstream looks remarkably similar... this is by design. In fact, you can (if you are careful) use C++ streams to swap between writing to screen, string buffers, or even files - even on the fly at runtime.

Share this post


Link to post
Share on other sites

That gets me past dealing with trying to concatenate strings and char values. Now, could you throw a couple of ints into the mix?


You use std::stringstream.

std::stringstream stream;
stream << number;
return stream.str();


'number' could be a float or an int.

I usually wrap that in a function, 'IntToString(int)' (though you could even make the function templated).

For your situation, I go like this:

std::string line = "(%1,%2)";
int x = 7, y = 12;

line = String::Replace(line, "%1", IntToString(x));
line = String::Replace(line, "%2", IntToString(y));


Where 'Replace' is a function I created (I like to have it in a 'String' namespace, but that's up to you).
The function looks like this:
//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;
}


[Edit:] I probably only do it this way, because I don't have experience with streams. You should do it ApochPiQ's way, since it's a standardized way, instead of trying to butcher the standardized way into some C-style way.

Share this post


Link to post
Share on other sites
stringstreams or boost::format are the best ways to go, boost::format being the much more flexible option

Share this post


Link to post
Share on other sites
Looks like stringstream is the way to go for me here.

I see a lot of people using boost libraries. I'm working on a cross-platform application. Should I consider using boost?

Share this post


Link to post
Share on other sites
Boost is cross-platform. Boost is a set of very good, very high-quality libraries. Think of it as an optional non-official extension to the standard library guided by the people who designed the standard library.

The first place you should look if you need something is the standard library, the second place is boost, then the third place is other 3rd party libraries on the internet.

Share this post


Link to post
Share on other sites
The C-way of doing this would be snsprintf or vsnsprintf as these allow you to controll the size of the buffer that is being written into. StringStream is the C++ way of fixing it but you might encounter sprintf and it's ilk in older codebases so it is good to be aware of these functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!