Ansistring methods in BCB

Source: Internet
Author: User
The ansistring class is one of the most common classes in BCB. It will help you to learn more about this class in the future. It is summarized as follows. With my personal experience, I have made up an introduction to the most common method attributes of this class. I hope it will be helpful to you.

Common Methods:
1. char * c_str ()
Returns a string pointer pointing to the same content as the content of the string contained in ansistring (BCB is used to explain this method in help. For more details about it, see the following article)
Example:
Ansistring STR = "Hello world! ";
MessageBox (null, str. c_str (), "", mb_ OK); // display a message box

2. ansicompare (ansistring & RHs)
Operator = (ansistring & RHs)
Both are used to compare whether the content of two ansistrings is the same. The difference between the two is that when the content is equal, the former returns 0, while the latter returns true.

3. Int length ()
Very simple. returns the string length.

4. Char & operator [] (const int idx)
Returns the idx character of the string.
Note: This place does not know what BCB thinks. It may be to take care of the habits of Delphi programmers, but it has caused a little trouble for C programmers: it is incompatible with the array usage in C, that is to say, when idx is 0, BCB throws 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 equals to 'E' and not 'l'
PS. The introduction below, as long as it is about the X character, is the same as here and will not be repeated

5. ansistring substring (INT index, int count)
Returns the string starting from the index. The length is a count substring.
Example:
Ansistring STR = "Hello world! ";
MessageBox (null, str. substring (). c_str (), "", mb_ OK); // The displayed content is world.

6. Int pos (ansistring & substr)
Returns the string whose content is substr. If yes, returns the first character of the string. If no value exists, returns 0.

7. Int toint ()
Int tointdef (INT defaultvalue)
Returns the result of converting the current string to an integer. The difference is that when the conversion fails, the former throws an exception and the latter returns the defaultvalue.

8. ansistring lowercase ()
Ansistring uppercase ()
Returns the result of converting the current string to lowercase or uppercase.
Note: they only return a new ansistring, and the content of the original string has not changed.

9. ansistring & sprintf (char *,...)
Use the powerful sprintf function to format the current string. For detailed usage, see sprintf or printf.

Other operators: ++ ==><=! =
You don't need to explain this ......

In-depth discussion:

1. About the return value of c_str.
Can I use this return value to access/modify the content in the string?
Example:
Ansistring A = "I want to modify this using pointer ";
Char * P = A. c_str ();
P [0] = 'I ';
...... // Pointer operation
Showmessage ();
Although the help of BCB indicates that the return value (char *) of the c_str () function is only valid in its expression,
However, the above Code can achieve the expected purpose sometimes.
I strongly oppose using this method to change the content of the string, because the ansistring built-in method can already be
Effectively complete pointer operations
For example, in the above example, P [0] can be replaced by a [1 ].
The merged string can be replaced by + or + =.
You can also find the corresponding methods in the ansistring class in the help for search, insert, delete, and so on.

2. Unicode support
Because ansistring is not a template class
Except for the widechar method, ansistring does not seem to support Unicode
But I don't know why the bytetype method exists.

3. Interesting Methods
Unique ()
Lastdelimiter ()
Not commonly used, but I think it is a very interesting function.
For more information, see help.

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.