ANSISTRING Usage Method

zhaozj2021-02-16  51

ANSISTRING CHAR

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "Haha"; 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 STR = INTOSTR (i);}

ANSISTRING Double

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring Test = "123"; long double d = strtofloat (test);}

Double turn ANSISTRING

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Double D = 123.456; Ansistring Str = floattostr (d);}

Double transfer Ansistring and rounded

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {long double d = 123.456121212; Ansistring str = floattostrf (d, fffiXed, 5, 4); // Description FloatTostrf 5 represents the latter digit from the first few numbers Starting around, 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; Ansistring str = formatfloat ("000000.00", d);} // get 000123.45, of course you can use "#.,; E E- XX "Other symbols, you try it yourself: Wink:

Ansistring turn Tclor type

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring Test = "0x00FF8080"; TCOLOR COL = StringTocolor (TEST);} Tclor 转 Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {tcolor col = 0x00ff8080; ANSISTRING STR = ColorToString (col);}

Eliminate part of the string in ANSISTRING

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "Abcdef"; int first = 3; // Eliminate the beginning INT page = 2; // Eliminate the length ANSISTRING DSTR = Test.delete (First, Length);} // Get Abef

Insert a string in Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "Abcdef"; ANSISTRING INS = "12345"; // Insert Skewers INT POS = 3; // Insert Ansistring Istr = Test.Insert (INS) , POS); // Get ab12345cdef}

Get ANSI a bit of characters

Code: void __fastcall tform1 :: butt * sender) {ansistring test = "abcdef"; Ansistring npos = test [3]; // Get c}

Get the last character in Ansistring

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring 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 Ansistring GetStr = Test.Substring (First, Length); // Get CD}

ANSISTRING letter comparison

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring Test = "Abcdef"; Ansistring Sample = "Abcdef"; int result = test.ansicompare (Sample); return 1, different! Size. }

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring test = "abcdef"; ansistring sample = "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) {Ansistring Test = "Abcdef"; Ansistring Sample = "E"; 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) {ansistring test = "abcdef"; ansistring sample = "ef"; int result = Test.pos (Sample); // Return 5, the position of the E character}

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

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "Gold Coin"; int Len = Test.Length (); // Return 8}

Number of strings, similar VB's Left

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "Little Apple"; Ansistring Slstr = Test.setlength (6);} // Get "Little Apple"

Detect double-character string

Code: void __fastcall tform1 :: button1click (TOBJECT * Sender) {Ansistring Chkstr = "Hello"; int CHKPOS = 1; if (bytetype (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) {ANSISTRING TEST = ""; BOOL CHK = TEST.ISempty (); if (chk) Edit1-> text = "1"; // Return 1}

All lowercase vs all get bigcase

Code: void __fastcall TForm1 :: Button1Click (TObject * Sender) {AnsiString Test = "ABCdef"; AnsiString Lstr = Test.LowerCase ();} Code: void __fastcall TForm1 :: Button1Click (TObject * Sender) {AnsiString Test = "ABCdef "; Ansistring ustr = test.uppercase ();

Similar to the empty lattice function in VB

Code: void __fastcall TForm1 :: Button1Click (TObject * Sender) {AnsiString Test = "ABCDEF"; AnsiString TLstr = Test.TrimLeft (); AnsiString TRstr = Test.TrimRight (); AnsiString Tstr = Test.Trim ();}

However, this processing that does not have a full corner

Code: ANSISTRING __FASTCALL TFORM1 :: Trimstr (AnsistringTM, ANSISTRING LR) {// lr ... L: Left Remove R: Right Remove B: Dou Remove INT LEN; // Left Remove IF (LR == "L" || LR == "B") {len = tm.length (); while (tm.substring (1, 1) == "" || tm.substring (1, 2) == "") {/// half angle removal IF (TM.SUBSTRING (1,1) == "" "" {TM = Tm.Substring (2, len); len = tm.length ();} // Remove else if (Tm.Substring (1, 2) == "") {TM = Tm.SubString (3, len); len = tm.length ();}}} // Right removal IF (LR == "R" || LR == "B") { Len = tm.length (); while (tm.substring (len, 1) == "" || tm.substring (len-1, 2) == "") {// half angle of IF (TM.SUBSTRING) Len, 1) == "" "{len = tm.length (); TM = Tm.Substring (1, len-1); len = tm.length ();} // Remove else if (TM.SUBSTRI) Ng (len-1, 2) == "" "" {len = tm.length (); TM = Tm.Substring (1, len-2); len = tm.length ();}}} ReturnTM; Void __fastcall tform1 :: button1click (TOBJECT * Sender) {ANSISTRING TEST = "Fighter"; ANSISTRING RET = Trimstr (TEST, "B")} Duplicate input

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) {AnsiString Str = "Borland C Builder is free"; AnsiString From = "C Builder"; AnsiString To = "Delphi"; AnsiString Result; Result = StringReplace (Str, From , TO, TREPLACEFLAGS () << RFREPLACEALL << RFIGNORECASE); // << is the parameter, get borland delphi is free}

Full-angle

Code: 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;

Half-angle

Code: 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); ANSISUSTRING ZEN = Ansistring (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 a test of ANSISTRING"; intlene = Temp.Length (); for (int i = 1; i <= length; i ) {IF (TEMP [i]> 128) {///// ........ // Screen the Chinese characters; i ;} else {// ... Non-Chinese characters. }} There is no need to use C_STR () to convert to a string of C_STR () to convert to C. 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 << This operator, if you want to use us to join: #define vcl_iostream This 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-18084.html

New Post(0)