|
-
Mar 5th, 2001, 12:57 PM
#1
Thread Starter
Hyperactive Member
How do I go about concatenating two strings?
I know I should use lstrcat for it, but how?
I get a lot of Access Violation errors,
and the lstrcat just don't want to work.
For example...
I want to concatenate the strings "First " and "Second ", to form "First Second ".
Then I want to concatenate the result with "Third", to form "First Second Third".
I know it must not be that hard, but for me it is.
Help, anyone?
Thanx.
-
Mar 5th, 2001, 01:03 PM
#2
Monday Morning Lunatic
Try just using strcat. Or alternatively, my preferred method is to do it myself, or use the string class:
Code:
#include <iostream>
#include <string>
using std::cout;
using std::string;
void main() {
string x;
x = "First ";
x += "Second ";
x += "Third";
cout << x << endl;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 5th, 2001, 01:21 PM
#3
Thread Starter
Hyperactive Member
Thanx parksie.
You seem to know an awf lot.
But another problem arises.
I want to put the final result into a LPCTSTR string.
For example:
Code:
string x;
LPCTSTR MyStr;
// Do x string stuff
MyStr = x; // ????????? How now?
-
Mar 5th, 2001, 01:37 PM
#4
Monday Morning Lunatic
You shouldn't use LPCTSTR unless you want to have multiple-character set compatibility. You can access a const pointer to the string by using x.c_str() (a member function):
Code:
const char *pStr = x.c_str();
Or alternatively you can just copy it:
Code:
char *pStr = new char[x.length()+1];
strcpy(pStr, x.c_str);
pStr[x.length()] = 0;
Then use pStr as you would normally. For things like MessageBox you can simply pass the result of the function:
Code:
MessageBox(hWnd, text.c_str(), caption.c_str(), MB_OK);
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|