共用方式為


strlen, wcslen, _mbslen, _mbstrlen

Get the length of a string.

size_tstrlen(constchar*string);

size_twcslen(constwchar_t*string);

size_t_mbslen(constunsignedchar*string);

size_t_mbstrlen(constchar*string);

Routine Required Header Compatibility
strlen <string.h> ANSI, Win 95, Win NT
wcslen <string.h> or <wchar.h> ANSI, Win 95, Win NT
_mbslen <mbstring.h> Win 95, Win NT
_mbstrlen <stdlib.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error.

Parameter

string

Null-terminated string

Remarks

Each of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcslen strlen strlen wcslen
_tcsclen strlen _mbslen wcslen

_mbslen and _mbstrlen return the number of multibyte characters in a multibyte-character string. _mbslen recognizes multibyte-character sequences according to the multibyte code page currently in use; it does not test for multibyte-character validity. _mbstrlen tests for multibyte-character validity and recognizes multibyte-character sequences according to the LC_CTYPE category setting of the current locale. For more information about the LC_CTYPE category, see setlocale.

Example

/*  STRLEN.C */

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

void main( void )
{
   char buffer[61] = "How long am I?";
   int  len;
   len = strlen( buffer );
   printf( "'%s' is %d characters long\n", buffer, len );
}

Output

'How long am I?' is 14 characters long

String Manipulation RoutinesLocale Routines

See Also   setlocale, strcat, strcmp, strcoll Functions, strcpy, strrchr, _strset, strspn