AnsiString和各种数据类型间相互转换 [数据转换]

//Ansistring 转 char
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "哈哈";
    char *chr = Test.c_str();
}
//char转Ansistring
#include 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString str = "Sample";
    char chr[MAX_PATH];
    strcpy( chr , str.c_str() );
}
//Bool转AnsiString
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test=BoolToStr(CheckBox1->Checked);
}
//Ansistring转bool
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test="-1"
                    CheckBox1->Checked= StrToBool( Test );
}
//int转ansistring
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int i = 123;
    AnsiString str = IntToStr( i );
}
//AnsiString转double
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "123";
    long double d = StrToFloat( Test );
}
//double转AnsiString
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    double d = 123.456;
    AnsiString str = FloatToStr( d );
}
//double转AnsiString并四舍五入
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    long double d = 123.456121212;
    AnsiString str = FloatToStrF( d , ffFixed ,5 , 4 );
//说明FloatTostrF里5代表从第几个数字的后一位开始四舍五入,4代表取4位小数。
//执行后得到str是123.4600。:roll:
}
//double转AnsiString使用类似vb的format函数
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    double d = 123.456;
    AnsiString str = FormatFloat( "000000.00" , d );
}
 //得到 000123.45,当然你可以使用"# . , ; E+ E- xx"等符号,你自己试试 :wink:
//AnsiString转Tclor型
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "0x00FF8080";
    TColor Col = StringToColor( Test );
}
//Tcolor转AnsiString
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TColor Col = 0x00FF8080;
    AnsiString str = ColorToString( Col );
}
//消除AnsiString 中的一部分字符串 代码:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    int First = 3; // 消除制定开头
    int Length = 2;// 消除制定长度
    AnsiString Dstr = Test.Delete( First , Length );
}//得到ABEF
//在AnsiString 中插入字符串
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    AnsiString Ins = "12345"; // 插入串
    int Pos = 3;  // 在哪插
    AnsiString Istr = Test.Insert( Ins , Pos );
//得到AB12345CDEF
}
//取得ansi某一位字符
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    AnsiString Npos = Test[3];//得到C
}
//取得AnsiString里最后一个字符
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    char *Lstr = Test.AnsiLastChar();//得到F
}
//取出AnsiString字符,这个类似vb的mid函数!
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    int First = 3; // 3开始取
    int Length = 2;// 取2位
    AnsiString Getstr = Test.SubString( First , Length );
//得到CD
}
 //AnsiString的字母比较
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    AnsiString Sample = "abcdef";
    int Result = Test.AnsiCompare( Sample );
//返回1,不同!分大小写。
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    AnsiString Sample = "abcdef";
    int Result = Test.AnsiCompareIC( Sample );
//返回0,相同!没有分大小写,哈哈
}

copy from other

转载于:https://www.cnblogs.com/azbane/p/8710064.html

你可能感兴趣的:(AnsiString和各种数据类型间相互转换 [数据转换])