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

How to convert a string into a AnsiString? 1

Status
Not open for further replies.

UmaGrama

Technical User
Sep 8, 2003
33
BR
Hi,

I need to convert a regular string into a AnsiString so I can add it to a database table (it only takes it as AnsiStrings).

Anyone with any ideas? They are always a great help.

Thanks...
 
std::string mybasic;
String myansi;
mybasic = "this";
for(int t=0;t<mybasic.length();t++)
myansi += mybasic.at(t);



Chi Happens

Chi Happens
&quot;When you know and respect your own inner nature, you know where you belong. You also know where you don't belong&quot; - Benjamin Hoff
 
It's even easier if you use c_str().
Code:
AnsiString BorlandStr;
std::string StandardStr = &quot;This is a C++ string&quot;;
BorlandStr = StandardStr.c_str();

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
2ffat,
Excellent. I have used c_str to convert ansistring for use with api calls, but it didn't even occur to me to convert the standard string to a c_string.

lol, that ROCKS!!!


Chi Happens
&quot;When you know and respect your own inner nature, you know where you belong. You also know where you don't belong&quot; - Benjamin Hoff
 
Thanks a LOT guys!!!!!!!!!!!!!!!! It works perfectly and it is beautiful!!!! Man oh man... I am getting close to be done... oh yeah.

Thanks men!
 
Now I only have to figure out why it shows in the table on M. Access and not on my TDBGrid... craziness it is.

Thanks again.
 
Ok... and how can I do the other way around? AnsiString to String?
 
Just use c_str() on the ansistring, that converts it to a basic string.

String MyAnsiString = &quot;This is my string&quot;;
string MyBasicString;
MyBasicString = MyAnsiString.c_str();
Caption = MyBasicString.c_str();

Chi Happens
&quot;When you know and respect your own inner nature, you know where you belong. You also know where you don't belong&quot; - Benjamin Hoff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top