16. Some specific usage of C/C ++ in CB
2) is ansistring introduced from Delphi?
Answer: The Core Component VCL of CB is written in the Object Pascal language. Therefore, many of the VCL components of CB use long string, for example: text, name, caption, and other attributes use the long string of Object Pascal. Based on this relationship, CB had to establish a class corresponding to the long string of Object Pascal, which we call ansistring.
3) What is the difference between ansistring and string?
A: C ++ builder has the following definitions in the sysdefs. h header file:
Typedef ansistring string;
From this point of view, the two are completely the same, but it is easier to write the latter, and the former is more clear.
4) Can you introduce some common functions of the ansistring class and their usage?
A: Of course. The following describes common functions:
Member Functions
Syntax
Function
C_str
Char * _ fastcall c_str () const
Returns a pointer to string data.
Delete
Void _ fastcall Delete (INT index, int count)
Delete count characters from Index
Insert
Void _ fastcall insert (const ansistring & STR, int index)
Insert STR to the original string starting from Index
Isempty
Bool _ fastcall isempty () const
Whether the returned string is null. True indicates a null string.
Length
Int _ fastcall length () const
Returns the string length.
Lowercase
Ansistring _ fastcall lowercase () const
Rewrite the uppercase letters in the string to lowercase letters.
Uppercase
Ansistring _ fastcall uppercase () const
Rewrite lowercase letters in a string to uppercase letters.
Pos
Int _ fastcall pos (cont ansistring & substr) const;
Locate the starting position of the substring in the original string.
Substring
Ansistring _ fastcall substring (INT index, int count) const
Returns the Count character from the index.
Todouble
Double _ fastcall todouble () const
Convert a character to a double-precision value
Toint
Int _ fastcall toint () const
Convert character to integer
Inttostr
Ansistring _ fastcall inttostr (INT value)
Convert an integer to a string
Trim
Anststring _ fastcall trim () const
Returns a new string, which will be left blank before and after the original string or clear control characters before and after returning
Widechar
Wchar_t fastcall widechar (wchart * DEST, int destsize) const
Convert ansistring to a wide character array (commonly used in COM)
5) What are the differences between anststring member functions and Traditional string functions?
A: You can view the differences between them through a comparison table:
Ansistring member functions
Function
Traditional string functions
=
String Copy
Strcpy
+ =
String Merging
Strcat
+
String connection
None
= ,! + =, <, <=,>,> =
String comparison
Strcmp
C_str ()
Mutual Conversion
None, but can be implemented using pointers
Delete
Delete a substring
None
Insert
Insert a substring
None
Length
Evaluate the string length
Strlen
Lowercase
Lowercase letters
Strlwr
Pos
Locate the substring
Strstr
Setlength
Set String Length
None
Toint
Convert to integer
None
Todouble
Convert to Double Precision
Sprintf
Uppercase
Uppercase letters
Strupr
6) how to convert traditional strings and ansistring strings?
A: The traditional C language uses characters to form a string ('/0' must be at the end of the string). The format is different from that of ansistring. Since I use the ansistring format in CB in large quantities, sometimes some conversions are inevitable. We can implement these three methods:
Method 1: Convert ansistring to a string array:
First, use the c_str () method in the ansistring category to convert it to a traditional string array, and then copy it to strcpy to copy it to the character array:
Char S1 [20];
Strcpy (S1, edit1-> text. c_str ());
Method 2: Converting ansistring to a traditional string can be achieved using the character pointer:
It can be achieved through the character pointer:
Char * s;
S = edit-> text. c_str ();
Method 3: convert a traditional string to an ansistring using a character pointer:
Char * s = "Try it and see if it can be successful !";
Edit-> text = s;
(To be continued)