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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char -> string

Status
Not open for further replies.

Jerrie85

IS-IT--Management
Feb 10, 2004
29
CA
how would i convert a char array to string?

e.g string temp;
char chararray[100];

temp should have chararray's contents from 0 to 500

how would you do this?
 
If the char array is null terminated, you can just assign it:
Code:
string temp = chararray;
// or
string two;
two = chararray;
If the array is not null terminated, or you only want to assign the first 50 characters, you can do like this:
Code:
string temp(chararray, chararray + 50);
string two;
two.assign(chararray, chararray + 50);
 
how would you assign it to the string from the nth position to the ith position, instead of from 0 to n?

so for a char[1000], i wanna assign to string from position 3 - 10?

 
Convert that to a CString and after that you can do whatever manipulations you want with that...


Cheers,

Ravi
 
Jerrie85 said:
how would you assign it to the string from the nth position to the ith position, instead of from 0 to n?

so for a char[1000], i wanna assign to string from position 3 - 10?
Ahh, c'mon, you can figure it out! :)

... Ok, here you go:
Code:
char chararray[1000];
// ... load chararray
string temp(chararray + 3, chararray + 10);
string two;
two.assign(chararray + 3, chararray + 10);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top