ansistring使用大全

ansistring 使用大全:

顾名思义,就是利用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,相同!没有分大小写,哈哈 
}


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

 

//转换为utf-8

 

AnsiString   sAnsi   =   "123";   
UTF8String   sUtf8;   
    
  sUtf8   =   AnsiToUtf8(sAnsi);

你可能感兴趣的:(ansistring使用大全)