How to convert Char to CString

Sep 16, 2008 at 6:18am
I have a custom little CString formatter which lets you insert variables into a CString. It works as follows:

you have a reference to a main string on a seperate string table, and then secondary CStrings which are string representations of variables and fit in the first CString in order. eg:

if:
sMainString = "I have three %1 and two %2"
sString 1 = "apples"
sString 2 = "oranges"

i can write:
CString = construct::makeString(sMainString, sString1, sString2);

and CString will become:

Cstring = "I have three apples and two oranges"

Now, my problem is that the variables after the comma MUST be CStrings.
If I have:
sMainString = "The letter '%1' is the letter you have chosen in %2"
int iChoice = 4
sWord = "people"

In this case, instead of 2 CStrings, i have a CString called sWord which lets say has a value of "people", sWord is turned into a char * array so i can find the 4th (iChoice) letter.

I now have:
CString = construct::makeString(sMainString, sWord(iChoice), (const char *)sWord);

how do I convert sWord(iChoice) and (const char *)sWord into CStrings?

(No i can't use CString.Format because the first string is a reference on a string table and it won't work)
Last edited on Sep 16, 2008 at 6:20am
Sep 16, 2008 at 7:05am
Um... What's the problem with sprintf(), again?

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html
Sep 18, 2008 at 12:24am
The fact that it can't pick the order of the items you put in.

eg:

I Like %s, they are %s.

lets say the first string is apples, and the second is sweet.

Like that, I can say

I like apples, they are sweet.

But what if i want to change that string to:

I like sweet fruit, especially apples.

I can't do that.

if i number the strings %1, %2 i can:

I like %1, they are %2

and then i can change the string to:

I like %2 fruit, especially %1.
Sep 18, 2008 at 12:47am
Sure you can.
1
2
3
4
5
6
7
8
9
10
//char *a,*b,*res;
char *c,*d;
if (condition){
	c=a;
	d=b;
}else{
	c=b;
	d=a;
}
sprintf(res,"%s%s",c,d);

Of course, nobody forces you to use this. You can just go ahead and reinvent the wheel.
Topic archived. No new replies allowed.