分享
 
 
 

AnsiString 使用大全

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

//Ansistring 转 char

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "哈哈";

char *chr = Test.c_str();

}

//char转Ansistring

#include <windef.h>

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,相同!没有分大小写,哈哈

}

//在AnsiString中寻找字符

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "ABCDEF";

AnsiString Sample = "E";

int Result = Test.Pos( Sample );

//返回5,如果你写Sample="haha",就返回0,找不到,哈哈

}

//在AnsiString中找字符串,和上一个类似

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "ABCDEF";

AnsiString Sample = "EF";

int Result = Test.Pos( Sample );

//返回5,即E字符的位置

}

//判断字符串长度,类似vb的len

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "拿金币来";

int Len = Test.Length();

//返回8

}

//取得字符串,类似vb的left

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "小苹果然看了这篇文章";

AnsiString SLstr = Test.SetLength(6);

}//得到"小苹果"

//检测双字节字串

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString ChkStr = "你好";

int ChkPos = 1 ;

if ( ByteType( ChkStr , ChkPos ) == mbSingleByte ){

Edit1->Text="0";

}

else{

Edit1->Text="1";

}//返回1,如果你写ChkStr="fxxk",就返回0

}

//检测空字符串

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "";

bool chk = Test.IsEmpty();

if (chk )

Edit1->Text="1";//返回1

}

//全部变小写vs全部变大写

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "ABCdef";

AnsiString Lstr = Test.LowerCase();

}

代码:

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "ABCdef";

AnsiString Ustr = Test.UpperCase();

}

//类似vb中Trim 的去空格函数

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = " ABCDEF ";

AnsiString TLstr = Test.TrimLeft();

AnsiString TRstr = Test.TrimRight();

AnsiString Tstr = Test.Trim();

}

//但是,这个处理不了全角的空格 代码:

AnsiString __fastcall TForm1::TrimStr( AnsiString Tm , AnsiString LR )

{

// LR … L:左除去 R:右除去 B:dou除去

int len;

// 左除去

if ( LR == "L" || LR == "B" ){

len = Tm.Length();

while ( Tm.SubString(1,1) == " " || Tm.SubString(1,2) == ""){

// 半角除去

if ( Tm.SubString(1,1) == " " ){

Tm = Tm.SubString(2,len);

len = Tm.Length();

}

// 全角除去

else if ( Tm.SubString(1,2) == "" ){

Tm = Tm.SubString(3,len);

len = Tm.Length();

}

}

}

// 右除去

if ( LR == "R" || LR == "B" ){

len = Tm.Length();

while ( Tm.SubString(len,1) == " " || Tm.SubString(len-1,2) == "" ){

// 半角除去

if ( Tm.SubString(len,1) == " " ){

len = Tm.Length();

Tm = Tm.SubString(1,len-1);

len = Tm.Length();

}

// 全角除去

else if ( Tm.SubString(len-1,2) == "" ){

len = Tm.Length();

Tm = Tm.SubString(1,len-2);

len = Tm.Length();

}

}

}

return Tm;

}

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Test = "拳脚";

AnsiString Ret = TrimStr(Test,"B")

}

//相同字符重复输入

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Soc = AnsiString::StringOfChar( '*' , 100 );

Edit1->Text=Soc ;//显示100个*

}

//字符串替换

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Str = "Borland C++ Builder is free";

AnsiString From = "C++ Builder";

AnsiString To = "Delphi";

AnsiString Result;

Result = StringReplace( Str, From, To, TReplaceFlags() <<

rfReplaceAll << rfIgnoreCase );

//<<后是参数,得到Borland Delphi is free

}

//全角变半角

AnsiString Zen = "1234567890";

int Len = Zen.Length();

char buf[MAX_PATH];

ZeroMemory( buf, sizeof( buf ) );

LCMapString( GetUserDefaultLCID(), LCMAP_HALFWIDTH, Zen.c_str(), Len, buf, sizeof( buf ) );

AnsiString Han = AnsiString( buf );

Edit1->Text=Han;

//半角变全角

void __fastcall TForm1::Button1Click(TObject *Sender)

{

AnsiString Han = "1234567890";

int Len = Han.Length();

char buf[MAX_PATH];

ZeroMemory( buf, sizeof( buf ) );

LCMapString( GetUserDefaultLCID(), LCMAP_FULLWIDTH, Han.c_str(), Len, buf, sizeof( buf ) );

AnsiString Zen = AnsiString( buf );

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有