1. 程式人生 > >計算字串長度、字串賦值

計算字串長度、字串賦值

以下全部討論char,wchar_t的請自行查閱msdn.以下函式的更詳細資訊也請查閱msdn

一:計算字串長度

1.sizeof:

        sizeof  unary-expression
	sizeof  (type-name)
示例程式碼:
	int a[]={0,1,2......};
	char b[5]="";
	int arraySize=sizeof(a)/sizeof(a[0])or sizeof(a)/sizeof(int);
	sizeof(b);

結果:
    陣列的長度和字串相應的長度
說明:
    計算字串時包含空字元位置


2. strlen:

	size_t strlen(
	   const char *str
	);
	str:Null-terminated string.

示例程式碼:
        char source[100] = "abcdefghijk";
	char des[8];
	int length1 = strlen(source);
	int length2 = strlen(des);

結果:
    length1=11;length2=27(此處值不穩定)
說明:
    此處des沒有被初始化,顧不正常.如果在宣告des後加上memset(des,0,8);或者char des[8]={0}or=""or="\0"
length2=0;此時正常.
    如果宣告為char des[8]={1};此時length2=1,但輸出des結果不是1,可自行測試.
此函式計算字串長度時不包含空字元位置
注意:
    char des[8]='\0'這麼寫的話,不是初始化


3. strnlen、strnlen_s:

	size_t strnlen(
	   const char *str,
	   size_t numberOfElements 
	);
	size_t strnlen_s(
	   const char *str,
	   size_t numberOfElements 
	);
示例程式碼:
        char source[100] = "abcdefghijk";
	int length=strnlen_s(source,num);

結果:
    length=num>strlen(source)?strlen(source):num;
    (strlen(source)=11)
注意:
    strnlen is not a replacement for strlen; strnlen is intended to be used only to calculate the size of incoming untrusted data in a buffer of known size—for example, a network packet. strnlen calculates the length but doesn't walk past the end of the buffer if the string is unterminated. For other situations, use strlen.


二:字串賦值

1.strcpy_s

	errno_t strcpy_s(
	   char *strDestination,
	   size_t numberOfElements,
	   const char *strSource 
	);
	strDestination:Location of the destination string buffer.
	numberOfElements:Size of the destination string buffer in char units for narrow and multi-byte functions, and wchar_t 
                         units for wide functions.
	strSource:Null-terminated source string buffer.
示例程式碼:
	char source[] = "abcdefghijk";
	char des[x] ="";
	strcpy_s(des+posDes,numberOfElements,source+posSou);

結果:
    自測
說明:
    source的起始位置是source[posSou],des的起始位置是des[posDes]
    1<=numberOfElements<=_countof(source)-posSou<=_countof(des)-posDes
     _countof為計算靜態陣列中元素數目的巨集,包含空字元,此處跟sizeof的計算結果一樣


2.sprintf_s

	int sprintf_s(
	   char *buffer,
	   size_t sizeOfBuffer,
	   const char *format,
	   ... 
	);
	buffer:Storage location for output
	sizeOfBuffer:Maximum number of characters to store.
	format:Format-control string...Optional arguments to be formatted
	
示例程式碼:
	char source[12] = "abcdefghijk";
	char des[10]="" ;
	sprintf_s(des,sizeOfBuffer,"%s",source);

結果:
    報錯
說明:
    此函式不夠安全,可用_snprintf_s


3._snprintf_s

	int _snprintf_s(
	   char *buffer,
	   size_t sizeOfBuffer,
	   size_t count,
	   const char *format [,
	   argument] ... 
	);
	buffer:Storage location for the output.
	sizeOfBuffer:The size of the storage location for output.Size in bytes for _snprintf_s or size in words for _snwprintf_s.
	Count:Maximum number of characters to store, or _TRUNCATE.
	format:Format-control string
	argument:Optional arguments.
示例程式碼:
	char source[] = "abcdefghijk";
	char des[x]="" ;
	_snprintf_s(des+pos,sizeOfBuffer,count,"123%s",source);

結果:
    自測
說明:
    1<=sizeOfBuffer<=_countof(des)-pos
    最多可拷貝count個字元到des中,起始位置為des[pos],count不含空字元位置,count<=sizeOfBuffer-pos-1
注意:
    des[pos]前面的字元要被賦值,否則輸出des為空,如果des沒有被初始化,des[pos]前的字元為亂碼.