C++ builder(BCB) Ansistring(字元、字串、數字等互轉+常用運算)

C++ builder(BCB) Ansistring(字元、字串、數字等互轉+常用運算)

C++ builder(BCB) Ansistring(字元、字串、數字等互轉+常用運算)



資料來源:http://blog.xuite.net/apo.chen/blog/38803287



AnsiString 轉 char

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString Test = "哈哈";
    char *chr = Test.c_str();
}


   


char轉AnsiString

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"等符號,你自己試試 😉
 



AnsiString轉Tclor型

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




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


 

AnsiString重載了“[]”運算符,可以將內容當做字元陣列一樣操作,不過和char[]不同的是AnsiString的[]運算符的下標是從1 開始的,和Delphi的String 相容。對於長度不大的AnsiString可以直接使用[]來操作,長度大的時候最好轉換成char*在使用,因為重載是通過成員函數實現的,增加了系統的調用開銷。
如:

AnsiString  Temp = "這是一個測試的AnsiString";
int Length = Temp.Length();
for(int i = 1; i <= Length;i++)
{
    if(Temp[i] > 128)
    {
         //........
         //篩選其中的漢字;
         i++;
    }
    else
    {
         //......非漢字的字元。
    }
}




AnsiString的構造函數有以下幾種:[各種 變數形態 直接轉字串的方法]

__fastcall AnsiString();
__fastcall AnsiString(const char* src);
__fastcall AnsiString(const AnsiString& src);
__fastcall AnsiString(const char* src, unsigned char len);
__fastcall AnsiString(const wchar_t* src);
__fastcall AnsiString(int src);
__fastcall AnsiString(double src);

__fastcall AnsiString(char src);
__fastcall AnsiString(short);
__fastcall AnsiString(unsigned short);
__fastcall AnsiString(unsigned int);
__fastcall AnsiString(long);
__fastcall AnsiString(unsigned long);
__fastcall AnsiString(__int64);
__fastcall AnsiString(unsigned __int64);
__fastcall AnsiString(const WideString &src);

 

 

 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *