Ansistring type conversion and use

Source: Internet
Author: User
// Convert ansistring to charvoid _ fastcall tform1: button1click (tobject * sender) {ansistring test = "Haha"; char * CHR = test. c_str ();} // Char to ansistring # include <windef. h> void _ fastcall tform1: button1click (tobject * sender) {ansistring STR = "sample"; char CHR [max_path]; strcpy (CHR, str. c_str ();} // convert bool to ansistringvoid _ fastcall tform1: button1click (tobject * sender) {ansistring test = booltostr (checkbox1-> C Hecked);} // ansistring to boolvoid _ fastcall tform1: button1click (tobject * sender) {ansistring test = "-1" checkbox1-> checked = strtobool (TEST );} // convert int to ansistringvoid _ fastcall tform1: button1click (tobject * sender) {int I = 123; ansistring STR = inttostr (I );} // convert ansistring to doublevoid _ fastcall tform1: button1click (tobject * sender) {ansistring test = "123"; Long Double D = strtofloat (TEST);}/double Convert ansistringvoid _ fastcall tform1: button1click (tobject * sender) {double D = 123.456; ansistring STR = floattostr (d );} // convert double to ansistring and rounding void _ fastcall tform1: button1click (tobject * sender) {Long Double D = 123.456121212; ansistring STR = floattostrf (D, fffixed, 5, 4); // indicates that in floattostrf, 5 represents rounding from the last digit of the number, and 4 represents taking four decimal places. // After execution, the STR value is 123.4600.: Roll:} // convert double to ansistring using the format function 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 "and other symbols. Try to convert ansistring to tclor void _ fastcall tform1: button1click (tobject * sender) {ansistring test =" 0x00ff8080 "; tcolor Col = stringtocolor (TEST);} // convert tcolor to ansistringvoid _ fastcall tform1 :: Button1click (tobject * sender) {tcolor Col = 0x00ff8080; ansistring STR = colortostring (COL);} // remove part of the string code in ansistring: void _ fastcall tform1 :: button1click (tobject * sender) {ansistring test = "abcdef"; int first = 3; // remove the specified start int length = 2; // remove the specified length ansistring dstr = test. delete (first, length);} // get ABEF // Insert the string void _ fastcall tform1: button1click (tobject * sender) in ansistring {ansistring te St = "abcdef"; ansistring ins = "12345"; // insert string int Pos = 3; // where to insert ansistring istr = test. insert (INS, POS); // get ab12345cdef} // obtain a certain ANSI character void _ fastcall tform1: button1click (tobject * sender) {ansistring test = "abcdef"; ansistring NPOs = test [3]; // obtain c} // obtain the last character void _ fastcall tform1 in ansistring :: button1click (tobject * sender) {ansistring test = "abcdef"; char * lstr = test. ansilastchar (); // get f }// Retrieve the ansistring character. This is similar to the VB mid function! Void _ fastcall tform1: button1click (tobject * sender) {ansistring test = "abcdef"; int first = 3; // 3 Start With int length = 2; // obtain two ansistring getstr = test. substring (first, length); // get CD} // compare the ansistring letters with void _ fastcall tform1: button1click (tobject * sender) {ansistring test = "abcdef "; ansistring sample = "abcdef"; int result = test. ansicompare (sample); // return 1, different! Case Sensitive .} Void _ fastcall tform1: button1click (tobject * sender) {ansistring test = "abcdef"; ansistring sample = "abcdef"; int result = test. ansicompareic (sample); // return 0, same! No case sensitivity, haha}

Unsigned char * C1 = (STR). c_str ();
The number of times it has been said. Do not use it like this.

Not to mention the ansistring and char * problems, I think Beginners should first remember the memory management. In most cases, whoever allocates (new) will be released (delete ).
Ansistring is a class that implements memory management internally, so you do not have to ignore it.
Char * is a pointer pointing to a piece of memory, so the user must be responsible for its allocation and release.
Ansistring. c_str () returns the memory managed by the class object. The address 1st may change, and the 2nd block memory operations may damage the class structure.

Not using intermediate variables is just a habit, and it is not always possible to do so, at least in the syntax.
Just like naming all variables a, B, c... the same is true, but it is not recommended to do so, and there are almost no programs to do so, except for some unfriendly small examples.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.