常用字串長度計算函數)

來源:互聯網
上載者:User
字串的長度通常是指字串中包含字元的數目,但有的時候人們需要的是字串所佔位元組的數目。常見的擷取字串長度的方法包括如下幾種。1.使用sizeof擷取字串長度sizeof的含義很明確,它用以擷取字元數組的位元組數(當然包括結束符\0)。對於ANSI字串和UNICODE字串,形式如下:
  1. sizeof(cs)/sizeof(char) 
  2. sizeof(ws)/sizeof(wchar_t)
可以採用類似的方式,擷取到其字元的數目。如果遇到MBCS,如"中文ABC",很顯然,這種辦法就無法奏效了,因為sizeof()並不知道哪個char是半個字元。2.使用strlen()擷取字串長度strlen()及wcslen()是標準C++定義的函數,它們分別擷取ASCII字串及寬字元串的長度,如:
  1. size_t strlen( const char *string ); 
  2. size_t wcslen( const wchar_t *string );
strlen()與wcslen()採取\0作為字串的結束符,並返回不包括\0在內的字元數目。3.使用CString::GetLength()擷取字串長度CStringT繼承於CSimpleStringT類,該類具有函數:
  1. int GetLength( ) const throw( );
GetLength()返回字元而非位元組的數目。比如:CStringW中,"中文ABC"的GetLength()會返回5,而非10。那麼對於MBCS呢?同樣,它也只能將一個位元組當做一個字元,CStringA表示的"中文ABC"的GetLength()則會返回7。4.使用std::string::size()擷取字串長度basic_string同樣具有擷取大小的函數:
  1. size_type length( ) const; 
  2. size_type size( ) const;
length()和size()的功能完全一樣,它們僅僅返回字元而非位元組的個數。如果遇到MCBS,它的表現和CStringA::GetLength()一樣。5.使用_bstr_t::length()擷取字串長度_bstr_t類的length()方法也許是擷取字元數目的最佳方案,嚴格意義來講,_bstr_t還稱不上一個完善的字串類,它主要提供了對BSTR類型的封裝,基本上沒幾個字串操作的函數。不過,_bstr_t 提供了length()函數:
  1. unsigned int length ( ) const throw( ); 
該函數返回字元的數目。值得稱道的是,對於MBCS字串,它會返回真正的字元數目。現在動手編寫如下程式,體驗擷取字串長度的各種方法。【程式 4-8】各種擷取字串長度的方法
  1.   #include "stdafx.h" 
  2.   #include "string" 
  3.   #include "comutil.h" 
  4.   #pragma comment( lib, "comsuppw.lib" ) 
  5.    
  6.   using namespace std; 
  7.    
  8.   int main() 
  9.   { 
  10.       char s1[] = "中文ABC"; 
  11.       wchar_t s2[] = L"中文ABC"; 
  12.    
  13.       //使用sizeof擷取字串長度  
  14.       printf("sizeof s1: %d\r\n", sizeof(s1)); 
  15.       printf("sizeof s2: %d\r\n", sizeof(s2)); 
  16.    
  17.       //使用strlen擷取字串長度  
  18.       printf("strlen(s1): %d\r\n", strlen(s1)); 
  19.       printf("wcslen(s2): %d\r\n", wcslen(s2)); 
  20.    
  21.       //使用CString::GetLength()擷取字串長度  
  22.       CStringA sa = s1; 
  23.       CStringW sw = s2; 
  24.    
  25.       printf("sa.GetLength(): %d\r\n", sa.GetLength()); 
  26.       printf("sw.GetLength(): %d\r\n", sw.GetLength()); 
  27.    
  28.       //使用string::size()擷取字串長度  
  29.       string ss1 = s1; 
  30.       wstring ss2 = s2; 
  31.    
  32.       printf("ss1.size(): %d\r\n", ss1.size()); 
  33.       printf("ss2.size(): %d\r\n", ss2.size()); 
  34.    
  35.       //使用_bstr_t::length()擷取字串長度  
  36.       _bstr_t bs1(s1); 
  37.       _bstr_t bs2(s2); 
  38.    
  39.       printf("bs1.length(): %d\r\n", bs1.length()); 
  40.       printf("bs2.length(): %d\r\n", bs2.length()); 
  41.    
  42.       return 0;
  43. }
輸出結果:sizeof   s1:  8sizeof   s2:  12strlen(s1):  7wcslen(s2):  5sa.GetLength():  7sw.GetLength():  5ss1.size():  7ss2.size():  5bs1.length():  5bs2.length():  5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.