To begin with brief information
AnsiString - a class of a dynamic line in capacity to 232-1 characters. As basis for creation of this class served паскалевский type String which has been expanded according to C possibilities ++.
Its singularity is that two copies of this class can physically occupy the same section of storage. The copy of this class contains the counter of links to it when this counter is nullified, the copy is automatically destroyed. If to try to change a copy to which it is had more than one reference the new copy of this line which will change will be created. It reduces портебность programs in storage, but also reduces processing rate.
Heritage of Pascal is also that characters in it are numbered with 1, instead of with 0 as it is accepted in a C/C ++
Creation of a line
Thanks to presence of the overloaded designers the line can be created from a considerable quantity of basic types of language of a C ++, for example:
AnsiString empty_string; /* blank line */
AnsiString one = 1; /* the line one will contain "1" */
AnsiString one_and_half = 1.5;/* in one_and_half the line "1.5" */contains
AnsiString one_charachter = ' a ';/* in one_charachter the line "a" */contains
AnsiString dup_string = AnsiString:: StringOfChar (' A ', 5);/* dup_string receives value "AAAAA" */
AnsiString simply_string = "string";
AnsiString shorten ("the Long line", 7);/* in shorten the first 7 */characters get only
Obtaining of the character from a line
To receive the character from a line of type AnsiString it is possible by means of the index operator:
AnsiString ansi_string = "Line";
char character = ansi_string [1];/* the very first */character is received
ansi_string [6] = ' and '; /* 6th is replaceable on a letter ' and ' */
Obtaining of the character array containing value AnsiString
Functions of library of execution of a C and function WinAPI demand, that lines were transferred to them in the form of character arrays. In AnsiString functions c_str and data are for this purpose intended. A difference in between that for blank line c_str returns "", and data - NULL. This difference can be illustrated by a following example:
AnsiString ansi_string;
char * str1 = ansi_string.c_str ();
char * str2 = (char *) ansi_string.data ();
Copying of value AnsiString in character array Unicode
Function WideChar is for this purpose intended, and the size of a demanded array can be learned, used function WideCharBufSize
AnsiString ansi_string = "the Line AnsiString";/* our line */
wchar_t * UnicodeString = new wchar_t [ansi_string. WideCharBufSize ()];/* an array-receiver */
ansi_string. WideChar (UnicodeString, ansi_string. WideCharBufSize ());
//it is used the received line, then it is deleted
delete [] UnicodeString;
How to learn, how many characters in line?
For this purpose it is necessary to use function Length.
How to delete in line end gaps?
It is done by functions Trim, TrimLeft and TrimRight according to both ends, only from the beginning and only from the end.
Operation with fragments of a line
AnsiString предоставлякт possibility поиcка (Pos), insertions (Insert), removals (Delete) and copy derivings (SubString) a text fragment in line:
AnsiString ansi_string = "Line";
int index = ansi_string. Pos ("ка");/* we find the word termination */
if (index! = 0) {
ansi_string. Insert ("чеч", index);/* now the line looks like "Strochechka" */
ansi_string. Delete (index, 2); /* and now - "Line" */
}
How to reserve to a line the certain size
Sometimes happens it is necessary to reserve for a line space, sufficient for storage of a certain amount of characters. To make it it is possible by means of function SetLength