About ANSISTING Usage (transferred from CHINABCB)

zhaozj2021-02-16  54

ANSISTRING CHAR

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

Char turn ANSISTRING

Code: #include

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

?? Ansistring str = "sample";

?? CHAR CHR [MAX_PATH];

?? STRCPY (chr, str.c_str ());

}

Bool turn ANSISTRING

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring test = booltostr (checkbox1-> checked);}

Ansistring turn BOOL

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "- 1"? CheckBox1-> Checked = strobool (test);}

INT to ANSISTRING

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? INT i = 123;??????????????

ANSISTRING Double

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {????????????????????????????????????????????????????????????????????????????????????????????????????????????? Long double d = strtofloat (TEST);}

Double turn ANSISTRING

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Double D = 123.456;?????????????

Double transfer Ansistring and rounded

Code: void __fastcall TForm1 :: Button1Click (TObject * Sender) {long double d = 123.456121212; AnsiString str = FloatToStrF (d, ffFixed, 5, 4); // FloatTostrF described in the first few digits from 5 represents a???? The latter one starts to go all four, and 4 represents 4 digits. // Get STR is 123.4600 after execution. : roll:?

Double transfer Ansistring uses a VB's Format function

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Double d = 123.456;????????????????????? "@ 000000.00", d);} // Get 000123.45, of course you can use "#.,; E E- XX "and other symbols, you try: Wink:

Ansistring turn Tclor type

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Ansistring test = "0x00ff8080";?? Tcolor color = StringTocolor (Test);} tclor} Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Tcolor color = 0x00ff8080;??????????????????????????????????????????????

Eliminate part of the string in ANSISTRING

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? ANSISUSTRING TEST = "AbcDef";? INT first = 3 ;??? // Eliminate the beginning of the development?? Intlength = 2 ;?? // eliminate Developing length? Ansistring dstr = Test.delete (first, length);} // get ABEF

Insert a string in Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? ANSISUSTRING TEST = "Abcdef";??????? // Insert a string? INT POS = 3;???? ?? // Where to plug? ANSISTRING ISTR = TEST.INSERT (INS, POS); // Get ab12345cdef}

Get ANSI a bit of characters

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? ANSISTRING TEST = "Abcdef";?? ANSISTRING NPOS = TEST [3]; // Get c}

Get the last character in Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? ANSISUSTRING TEST = "Abcdef";????? Char * lstr = Test.ansilastchar (); // Get f}

Remove the ANSISTRING character, this similar VB's MID function!

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? ANSISTRING TEST = "Abcdef";?? Int first = 3 ;??? // 3 Start Take? Int length = 2 ;?? // Take 2 bits? ANSISUBSTRING (first, length); // Get CD}

ANSISTRING letter comparison

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? Ansistring test = "abcdef";????? Int result = test.ansicompare (Sample); return 1, Different! Size. }

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? ANSISUSTRING TEST = "Abcdef";???? Int result = test.ansicompareic (Sample); // Return 0, the same ! No casement, haha} look for characters in Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? ANSISUSTRING TEST = "Abcdef";????? Int result = test.pos (sample); // Return 5, if You write sample = "haha", return 0, can't find it, haha}

Find a string in Ansistring, and the previous

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? ANSISUSTRING TEST = "Abcdef";???? Int result = test.pos (sample); // Return 5, E character position}

Judging the length of the string, similar to the LEN of VB

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Ansistring test = "Take Gold Coins";? Int len ​​= Test.length (); // Return 8}

Number of strings, similar VB's Left

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Ansistring Test = "Xiao App I have seen this article";?????????????????????????????????????????????

Detect double-character string

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING Chkstr = "Hello"; int CHKPOS =? 1?; If (ByType (Chkstr, Chkpos) == MBSINGLEBYTE) {?? Edit1-> Text = " 0 ";??}? Else {? Edit1-> text =" 1 ";??} // Return 1, if you write Chkstr =" fxxk ", return 0}

Detect empty string

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {???????? I (chk)? Edit1-> text = "1"; // Return 1}

All lowercase vs all get bigcase

Code: void __fastcall TForm1 :: Button1Click (TObject * Sender) Code {AnsiString Test = "ABCdef";; AnsiString Lstr = Test.LowerCase ()????}: Void __fastcall TForm1 :: Button1Click (TObject * Sender) {? ANSISSIING TEST = "Abcdef";????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Similar to the empty lattice function in VB

Code: void __fastcall TForm1 :: Button1Click (TObject * Sender) {AnsiString Test =; AnsiString TLstr = Test.TrimLeft (); AnsiString TRstr = Test.TrimRight ()?? "ABCDEF??????"???? ;? ANSISTRING TSTR = Test.trim ();

However, this processing that does not have a full corner

Code: ANSISTRING __FASTCALL TFORM1 :: Trimstr (ANSISTRINGTMSTR (ANSISTRINGTMSTR (ANSISTRINGTMSTRING LR) {?? // lr ... l: Left removal?? R: Remove?? B: Dou removed? Int Len;?? // Remove? ? if (lr == "l" || lr == "b") {??? len = tm.length ();???? While (tm.substring (1,1) == "" | | TM.SUBSTRING (1, 2) == "" ") {????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????????????????}?????? // All angle removed? ???????????????????????????????????????????????????????????????????????? = TM.LENGTH ();??????}?? // Right Remove?? I (lr == "r" || lr == "b") {??? ??????? while (tm.substring (len, 1) == "" || tm.substring (len-1, 2) == "") {??? ?? // half angle removed????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.? ??? Else IF (TM.SUBSTRI) Ng (len-1, 2) == "" "" {????????????? Tm = tm.substring (1, len-2) ????????????}????? ReturnTM;} void __fastcall tform1 :: button1click (TOBJECT * Sender) { ?? Ansistring Test = "Fighter";???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {?? Ansistring Soc = Ansistring :: stringofchar ('*', 100);? Edit1-> Text = SOC; // Show 100 *} string replacement

Code: void __fastcall tform1 :: button1click (TOBJECT * sender) {?? ANSISSIING STR = "Borland C Builder IS Free"??????????????????? ???????????? Y? = StringReplace (STR, FROM, TO, TREPLACEFLAGS () << RFREPLACEALL << RFIGNORECASE); // <

Full-angle

Code:?????????????????????????? ZeromeMory (buf, sizeof (buf));?? Lcmapstring (GetUserDefaultlcid (), LcMap_HalfWidth, Zen.c_STR (), LEN, BUF, SIZEOF (BUF));?????????????? Edit1-> Text = HAN;

Half-angle

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {???????????????????? ZeromeMory (buf, sizeof) BUF);? lcmapstring (getUserDefaultlcid (), lcmap_fullwidth, han.c_str (), len, buf, sizeof (buf));?????????????????????????????????????????????????????????????????

Ansistring is overloaded "[]" operators, you can operate the content as a character array, but different from char [] The subscript of the ANSISTRING [] operator is started from 1, and the String of Delphi is compatible with Delphi. For ansistring that is small, you can use [] to operate directly, it is best to convert to char * when the length is large, because the overload is implemented by the member function, increasing the system's call overhead. Such as:

Code: ANSISTRING? TEMP = "This is an ANSISTRING" "INT longth = Temp.length (); for (int i = 1; i <= length; i ) {? F (Temp [i]> 128) ?? {????? // Screening the Chinese characters;????? I ;??}?? Else?? {???? ? // ...... Non-Chinese characters. ?}} There is no need to use C_STR () to convert to C string to C_STR (). AnsiString constructor takes about several: Code: __ 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); the first is the default constructor is used when the life of a AnsiString, such as

Code: ANSISTRING STR1; The second is to convert a string constant or character array or character pointer to ansistring constructor, which is based on the rule of the C string, that is, the '/ 0' character encountered by the first one. As an end character. Common applications are

Code: ANSISTRING STR2 = "Teststring"; The third is the standard copy constructor. When an assignment is executed, this constructor is used. The fourth is to specify the constructor of the length and source. He and the second difference are not the end character with '/ 0' characters, but according to the designated length, this constructor can break through the C language. The limitations and insufficient intensity of strings may be more valueful in practice, and most APIs return to char *, but they are not necessarily visible characters, or the '/ 0' can be included. Using this constructor to achieve copy of the content, although the second parameter is a Unisgned Char type, it is actually used to break through 256 restrictions. In a sense, this constructor can be avoided when we use BCB. NEW to allocate array data for CHAR types, in a local application, use ANSISTRING to save temporary char array data, do not need to releasing memory without considering what is abnormal, because Ansistring is able to release themselves of. The fifth constructor can be the ANSISSTRING can be converted directly to WCHAR_T *, which is possible to directly use = assignment to the Ansistring type directly. The sixth is the constructor that converts integer into a string, that is, to directly assign a value of an int type to ANSISTRING, and the result is the same as by INTTOSTR. Such as: Code: int Temp = 46573284; Ansistring str3 = INTOSTR (TEMP); and ANSISTRING STR4 = TEMP; the result is the same. Seventh to fifteenth and sixth are similar, the role is also quite. The sixteenth is to convert Windows WideString to ansistring constructor, which is to assign WideString to ANSISTRING without having to use other methods or APIs to convert, features and Ansistring (Wchar_t *). String and string are different. String is a class that is standard from C supports the class of strings, and we often use String in C .

Code: COUT <

<

But we can't use String like this, but String is actually overloaded.

#define vcl_iostream

This way we can:

Code: string str = "Hello World"; cout <

<

Similarly, we can also do this:

Code: string str; cin >> Str; when using STL, we often need String, but String does not provide a method directly converted to string, but we can:

Code: // -------------------------------------------------------------------------------------------------------- ---------------------------- #pragma hdrstop #include

#include

#define vcl_iostream

#include

/ / -------------------------------------------------------------------------------------------- ---------------------------

Using namespace std;

#pragma argsused

Int main (int Argc, char * argv [])

{

String str = "Hello World";

String ss (str.c_str ());

? cout <

• System ("pause");

? Return 0;

}

At this time we can use STL's power to handle String. Mix ANSISSTRING and C_STR () security issues by Nethobo When you use a function that returns a Ansistring type variable, when you want to write a return ANSISTRING variable (not a pointer is not a reference) function, or when you use a Ansistring variable as parameters Function, or when you use VCL controls throughout the day, when this Ansistring variable contains a long string, are you worried? Worried about the structure and sectors of the object, more worrying about the efficiency problems caused by the reproduction of large strings (allocated memory, memory replication, delete memory)? If you are a perfectionist or a more responsible program author compare the relationship program efficiency, I think you have the same feeling like me when I use. In addition, when you want to CHAR * bare string operations for a ANSISTRING variable, you use ANSISTRING :: c_str () to get the CHAR * pointer and then perform C string operation, then you Whether you will worry about whether you have conflict with ANSISTRING, of course, you don't want to use strcpy to copy (or efficiency issues). Below I discusses the efficiency of ANSISSTRING and its C_STR () security issues on the problems found in the programming in the past few days. The BCB provides a Unicode version of the Unicode version of class ANSISTRING that operates dynamic strings, and Ansistring is used when you use a string type in the VCL class. However, sometimes some problems are more convenient to handle some problems with CHAR * naked strings, especially in some coding and decoding algorithms. To this end, Ansistring provides a C_STR () function to return to the CHAR * pointer of its internal (WideString is c_bstr (), the problem is similar, and the following is only Ansistring and C_STR as an example). First let's take a look at the second question (it is directly related to the first question), look at the following code: Ansistring SRC = "Test Ansistring"; Ansistring Strtest = src; // Copy Construction CHAR * CP = strTest.c_STR () CP [0] = 't'; What is the value of Strtest and SRC after running? The result may be very different from what you expected, and the values ​​of both are "test anstring"! That is to say, the operation of CP [0] = 't' also changes the value of two ANSISSTRING variables. Why do this, press the CTRL mouse when executed, you will find that two of them points to the inner string! That is to say, when the copy construct (the same is true), it is not a copy of the internal string we think! Ok, until now we don't have to worry about the first question, there is no copy of the string, single is the structure of the object and the designer can't be placed (Ansistring only one DATA member variable).

转载请注明原文地址:https://www.9cbs.com/read-18221.html

New Post(0)