Use VC to obtain the length of a string

Source: Internet
Author: User

4.5.8 String Length

The length of a string usually refers to the number of characters in a string, but sometimes people need the number of bytes occupied by the string. The following methods are commonly used to obtain the length of a string.

1. Use sizeof to get the string length

The meaning of sizeof is clear. It is used to obtain the number of bytes (including the terminator 0, of course) of the character array ). For ANSI and Unicode strings, the format is as follows:

 
 
  1. sizeof(cs)/sizeof(char)  
  2. sizeof(ws)/sizeof(wchar_t

You can obtain the number of characters in a similar way. If you encounter MBCS, such as "Chinese ABC", it is obvious that this method cannot work, because sizeof () does not know which char is half a character.

2. Use strlen () to obtain the string length

Strlen () and wcslen () are functions defined by the Standard C ++. they obtain the length of the ASCII string and the width string, for example:

 
 
  1. size_t strlen( const char *string );  
  2. size_t wcslen( const wchar_t *string ); 

Strlen () and wcslen () Take 0 as the string Terminator, and return the number of characters not including 0.

3. Use cstring: getlength () to obtain the string length

Cstringt inherits from the csimplestringt class, which has functions:

 
 
  1. int GetLength( ) const throw( ); 

Getlength () returns the number of characters rather than bytes. For example, in cstringw, getlength () of "Chinese ABC" returns 5 instead of 10. What about MBCS? Similarly, it can only treat one byte as one character. getlength () of "Chinese ABC" expressed by cstringa returns 7.

4. Use STD: String: size () to obtain the string length.

Basic_string also has the function to get the size:

 
 
  1. size_type length( ) const;  
  2. size_type size( ) const

The functions of length () and size () are exactly the same. They only return characters rather than the number of bytes. If mcbs is encountered, it performs the same as cstringa: getlength.

5. Use _ bstr_t: length () to obtain the string length.

The length () method of the _ bstr_t class may be the best way to get the number of characters. Strictly speaking, _ bstr_t is not called a perfect string class, it mainly provides encapsulation of the BSTR type, basically a few string operation functions. However, _ bstr_t provides the length () function:

 
 
  1. unsigned int length ( ) const throw( );  

The number of characters returned by this function. It is commendable that for an MBCS string, it returns the actual number of characters.

Start now

Write the following program to experience various methods to get the string length.

[Program 4-8] various methods for obtaining the string length

 
 
  1. 01 # include "stdafx. H"
  2. 02 # include "string"
  3. 03 # include "comutil. H"
  4. 04 # pragma comment (Lib, "comsuppw. lib ")
  5. 05
  6. 06Using NamespaceSTD;
  7. 07
  8. 08IntMain ()
  9. 09 {
  10. 10CharS1 [] = "Chinese ABC ";
  11. 11Wchar_tS2 [] = l "Chinese ABC ";
  12. 12
  13. 13 // use sizeof to get the string length
  14. 14 printf ("sizeof S1: % d/R/N ",Sizeof(S1 ));
  15. 15 printf ("sizeof S2: % d/R/N ",Sizeof(S2 ));
  16. 16
  17. 17 // use strlen to get the string length
  18. 18 printf ("strlen (S1): % d/R/N", strlen (S1 ));
  19. 19 printf ("wcslen (S2): % d/R/N", wcslen (S2 ));
  20. 20
  21. 21 // use cstring: getlength () to obtain the string length
  22. 22 cstringa SA = S1;
  23. 23 cstringw Sw = S2;
  24. 24
  25. 25 printf ("Sa. getlength (): % d/R/N", SA. getlength ());
  26. 26 printf ("Sw. getlength (): % d/R/N", SW. getlength ());
  27. 27
  28. 28 // use string: size () to obtain the string length
  29. 29 string SS1 = S1;
  30. 30 wstring ss2 = S2;
  31. 31
  32. 32 printf ("ss1.size (): % d/R/N", ss1.size ());
  33. 33 printf ("ss2.size (): % d/R/N", ss2.size ());
  34. 34
  35. 35 // use _ bstr_t: length () to obtain the string length
  36. 36_bstr_t BS1 (S1 );
  37. 37 _ bstr_t bs2 (S2 );
  38. 38
  39. 39 printf ("bs1.length (): % d/R/N", bs1.length ());
  40. 40 printf ("bs2.length (): % d/R/N", bs2.length ());
  41. 41
  42. 42Return0; 43}

The output result is 4-16.

 
(Click to view the big chart) Figure 4-16 running result

========================================================== ===

The above is excerpted from section 4.5.8 of "pulse VC ++". For more information, see the source.

If you want to communicate with me, please click the following link to add me as a friend: http://student.csdn.net/invite.php? U= 113292 & C = 8913f87cffe7d533

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.