Summary of Ansistring Class Method in BCB

zhaozj2021-02-16  59

The Ansistring class is one of the most common classes in BCB. It is understood that it has a lot of problems in the future to learn BCB, and there are many problems that have been proposed in this class. Plus my personal experience, make out an introduction of the most common method attributes about this class, I hope to help everyone

Common methods:

CHAR * C_STR ()

Returns a string pointer, the content he pointed to and the content of the string included in the Ansistring is consistent (this is to explain this method in the BCB Help. About it more detailed discussion)

EXAMPLE:

Ansistring str = "Hello World!";

MessageBox (NULL, STR.C_STR (), "", MB_OK); // Displays a message box

2.Ansicompare (ANSISTRING & RHS)

Operator == (ANSISUSTRING & RHS)

It is used to compare whether the contents of the two Ansistring are the same, the difference between the two is when the content returns 0, and the latter returns True

3.int Length ()

Very simple, return the string length

4.char & operator [] (const Int IDX)

Returns the iDX characters in the string

Note: This place doesn't know how BCB thinks. It may be to take care of Delphi programmers, but give C programmers have a little bit of trouble: and the usage of c-in-array is not compatible, that is, when IDX is 0 At the time, BCB will throw an exception instead of returning the first character! To get the first character, IDX should be 1

EXAMPLE:

Ansistring str = "Hello World!";

Char Byte = STR [2];

// Byte is equal to 'E' and not equal to 'L'

PS. The following introduction, as long as it is about the word X characters, it is no longer repeated

5. ManSiString Substring (int index, int count)

Return from the INDEX character, the length is count substrings

EXAMPLE:

Ansistring str = "Hello World!";

MessageBox (NULL, STR.SUBSTRING (7, 5) .c_str (), "", mb_ok); // Display content is World

6.int Pos (ANSISUSTRING & SUBSTR)

Find content for substr strings, if there is, the first character position of the back string is not, return 0

7.int toint ()

INT TOINTDEF (Int defaultValue)

Returns the result of the current string to the integer, and the difference is that when it cannot be transformed, the former throws an exception, and the latter returns DefaultValue, 嘿嘿, I like the latter.

8.ansistring lowercase ()

Ansistring Uppercase ()

Returns the result of the current string to lowercase / uppercase

Note that they just return a new ANSISTRING, and the contents of the string have not changed.

9. ManSistring & Sprintf (char *, ...)

Format the current string using Sprintf powerful function, please refer to Sprintf or Printf for details.

Other operators: => => <<=! =

These don't explain it ...

discuss in depth:

1. Regarding the return value of c_str (). Can I use this return value to access / modify the contents in a string?

EXAMPLE:

Ansistring a = "I want to modify this using pointer";

Char * p = a.c_str ();

P [0] = 'I';

........ / / Pointer operation

ShowMessage (a);

Although BCB helps indicate that the return value of the function of c_str () This is only valid in its expression.

But the above code sometimes completes the expected purpose

Personally try to use this method to change the content of the string, because the method of Ansistring itself has been able to

Completion of pointer operation

For example, in the example above, P [0] can be replaced with A [1]

Merged strings can be replaced with or =

There are also findings, insert, delete, etc., can be implemented in the help in the help of the ANSISTRING class.

2.Unicode support

Because Ansistring non-template class

In addition to the WideChar method, Ansistring does not seem to support Unicode

But I don't know why there will be bytetype.

3. That is interesting

Unique ()

LastDelimiter ()

Not common, but I think it is very interesting function.

How to use, you can refer to your help

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

New Post(0)