Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenate a variable to a string

Status
Not open for further replies.

csripriya1

Programmer
May 13, 2003
35
0
0
US
Hi all,
I have a simple question about the concatenation.I want to concatenate a string and an integer to get another string.I checked in my C++ book and in google. I couldn't get the answer.Can anyone help me,Please.

Thanks for the time and help.
 
Use itoa() on the integer.
If you are using std::strings, then just add the result of the itoa to the string.

If char[], create a new char[] and itoa() the integer and store in the new array. Measure the length of the new array, and create a third the size of the first 2 + 1.
Copy the 2 chars into the new array, and add'\0'

hth
K
 
Try out a good ole C solution:

char buff[256] = "Time remaining: ";

char cat[256];

sprintf(cat, "%d", 101);

strcat(buff, cat);

printf(buff);
 
(Some exotic:)
string str("Number One:");
...
{
strstream s;
s << str << 1 << ends;
str = s.str();
}
 
Hi Kalisto,ArkM,sebastiani,
Thanks for your time and help.
 
wsprintf(NewString, &quot;%s %d&quot;, OldString, IntegerVariable);

Isnt it as simple as that?

-LittlePaul
 
Heres another one:

Code:
CString s;
int i=42;
s.Format(&quot;The ultimate answer=%i&quot;, i);


/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
if you use MFC, you can do like follow:

CString szResult = &quot;&quot;;
TCHAR szChar[100] = &quot;How are you &quot;;
int nSize = 100;

szResult.Format(&quot;%s%d&quot;,szChar,nSize);

do it,you can sucess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top