CSDN博客

img chensheng913

关于AnsiSting的使用大全

发表于2004/6/30 18:13:00  7259人阅读

分类: BCB

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() );
}
 


ansistring转int
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "123";
    int i = StrToInt( Test );
}
 

int转ansistring
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int i = 123;
    AnsiString str = IntToStr( i );
}
 

ansisting转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:
 


ansi转Tclor型(从现在开始AnsiSting简称ansi
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "0x00FF8080";
    TColor Col = StringToColor( Test );
}


Tclor转ansi
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TColor Col = 0x00FF8080;
    AnsiString str = ColorToString( Col );
}


ansi的一部分消除,哈哈,类似vb里的mid函数,不过是反的~
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    int First = 3;     // 消除制定开头
    int Length = 2;    // 消除制定长度
    AnsiString Dstr = Test.Delete( First , Length );
}//得到ABEF
 


ansi的一部分插入
代码:

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
}


取得ansi里最后一个字符
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    char *Lstr = Test.AnsiLastChar();//得到F
}


取出ansi字符,这才是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
}


ansi的字母 是否相同比较
代码:

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


在ansi中寻找字符
代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "ABCDEF";
    AnsiString Sample = "E";
    int Result = Test.Pos( Sample );
//返回5,如果你写Sample="haha",就返回0,找不到,哈哈
}


在ansi中找字符串,和上一个类似
代码:

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();
}
 

但是,这个处理不了全角的空格   怎么办ni~
代码:

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 );
}

0 0

相关博文

我的热门文章

img
取 消
img