String Concatenation C++ not Torque Script...
by Quinton Delpeche · in Torque 3D Professional · 03/06/2014 (4:23 am) · 7 replies
I am trying to find a more elegant way of concatenating a couple of strings in C++.
basically I have the following items:
- variable1 contains a string (e.g. Fred).
I need to concatenate "-Bob" and "-George" and "-Ted" to variable one ... so that my result will be:
Fred-Bob-George-Ted
This is a pretty simplistic example, but I think the point is made. I have looked at strcat, strcopy, wcscpy, and wcscat ... but I want to know if there is a more elegant way of doing this?
I am probably missing something very easy inside the engine that I can use ... if I have, I apologise ... but I am not happy with the above methods.
Edit:
Never mind, I decided to use Chilkat's free CkString object.
Much cleaner ... much better ... much easier.
basically I have the following items:
- variable1 contains a string (e.g. Fred).
I need to concatenate "-Bob" and "-George" and "-Ted" to variable one ... so that my result will be:
Fred-Bob-George-Ted
This is a pretty simplistic example, but I think the point is made. I have looked at strcat, strcopy, wcscpy, and wcscat ... but I want to know if there is a more elegant way of doing this?
I am probably missing something very easy inside the engine that I can use ... if I have, I apologise ... but I am not happy with the above methods.
Edit:
Never mind, I decided to use Chilkat's free CkString object.
CkString str;
str.append( variable1 );
str.append( "-Bob" );
str.append( "-George" );
str.append( "-Ted" );
// prints "Fred-Bob-George-Ted"
Con::printf( "%s", str.getString() );Much cleaner ... much better ... much easier.
About the author
Gobbo Games is an Independent Games Development company operating from Durban in South Africa. We believe in creating high-quality cost-effective games that remain true to the belief of Independent Game Developers around the world.
#2
Although I've become partial to StringBuilder when it's not overkill for the task:
Both were off the top of my head so syntax not guaranteed :P
EDIT: Third one for the hell of it:
03/06/2014 (8:01 am)
String myvar = "Fred"; myvar += "-Bob"; myvar += "-George"; myvar += "-Ted";
Although I've become partial to StringBuilder when it's not overkill for the task:
StringBuilder sb;
sb.append("Fred");
sb.append("-Bob");
sb.append("-George");
sb.append("-Ted");
String myStr = sb.end();Both were off the top of my head so syntax not guaranteed :P
EDIT: Third one for the hell of it:
char outStr[256]; dSprintf( outStr, 256, "%s%s%s%s", "Fred", "-Bob", "-George", "-Ted" );
#3
03/06/2014 (9:17 am)
Indeed you can use std::string (std::basic_string<char>) or Torque's String class which are both available in Torque 3D. To pass such strings to other functions that expect const char * use the c_str() method as such:String str = "Fred";
str += "-Bob";
str += "-George" + "-Ted";
Con::errorf("%s is having a naming crisis!", str.c_str());
#4
I knew I was missing something obvious. Thanks again.
03/06/2014 (9:30 am)
@Andrew and @Nathan: Thanks ... can't believe I missed that one. *shakes head*I knew I was missing something obvious. Thanks again.
#5
std::stringstream str;
str << "Fred";
str << "-Bob";
str << "-George":
str << "-Ted";
Con::errorf("%s", str.str().c_str());
03/06/2014 (10:23 am)
I use std::stringstream a lot.std::stringstream str;
str << "Fred";
str << "-Bob";
str << "-George":
str << "-Ted";
Con::errorf("%s", str.str().c_str());
#6
03/06/2014 (12:00 pm)
I must be weird as I always tend to use the ugly "c style" strings. :P
#7
Another EXTREMELY useful tool that I use a ton is String::ToString() which allows you to use sprintf formatting to get a String back.
03/06/2014 (12:01 pm)
Yeah for torque's String class the += operator is the way to go.Another EXTREMELY useful tool that I use a ton is String::ToString() which allows you to use sprintf formatting to get a String back.
String test = "This is a sample string containing 1+1 which is ";
test += String::ToString("%i", 1+1);
Con::printf("%s", test.c_str());
Torque Owner Richard Ranft
Roostertail Games