Compartilhar via


strlen, strlen_l, wcslen, wcslen_l, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l

Obter o tamanho de uma seqüência de caracteres, usando a localidade corrente ou uma localidade específica.Versões mais seguras dessas funções estão disponível; consulte strnlen, strnlen_s, strnlen_l, wcsnlen, wcsnlen_s, wcsnlen_l, _mbsnlen, _mbsnlen_l, _mbstrnlen, _mbstrnlen_l

size_t strlen(
   const char *str
);
size_t strlen_l(
   const char *str,
   _locale_t locale 
);
size_t wcslen(
   const wchar_t *str 
);
size_t wcslen_l(
   const wchar_t *str,
   _locale_t locale
);
size_t _mbslen(
   const unsigned char *str 
);
size_t _mbslen_l(
   const unsigned char *str,
   _locale_t locale
);
size_t _mbstrlen(
   const char *str
);
size_t _mbstrlen_l(
   const char *str,
   _locale_t locale
);

Parâmetros

  • str
    Seqüência de caracteres terminada com caractere nulo.

  • locale
    Localidade usar.

Valor de retorno

Cada uma dessas funções retorna o número de caracteres em str, excluindo terminal NULL. Nenhum valor retornado é reservado para indicar um erro, exceto para _mbstrlen, que retorna -1 se a seqüência de caracteres contém caractere inválido multibyte.

Comentários

strlen interpreta a seqüência de caractere sistema autônomo uma seqüência de caractere de byte único, portanto, seu valor retornado é sempre igual ao número de bytes, mesmo se a seqüência de caractere contém caractere de multibyte. wcslen é uma versão de caractere largo da strlen; o argumento do wcslen é uma seqüência de caracteres largos e a contagem de caracteres está em caracteres largos (dois byte). wcslen e strlen tenham comportamento idêntico caso contrário.

Observação de segurança    Essas funções incorrer em uma ameaça potencial colocada um problema de saturação de buffer.Problemas de saturação de buffer são um método de ataque do sistema, resultando em uma elevação do privilégio unwarranted freqüente.Para obter mais informações, consulte Evitar saturações de buffer.

Mapeamentos de rotina de texto genérica

Rotina TCHAR.H

_UNICODE & _MBCS não definido

_MBCS definido

_UNICODE definido

_tcslen

strlen

strlen

wcslen

_tcsclen

strlen

_mbslen

wcslen

_tcsclen_l

strlen_l

_mbslen_l

wcslen_l

_mbslen,_mbslen_l, _mbstrlenand_mbstrlen_l return the number of multibyte characters in a multibyte-character string but they do not test for multibyte-character validity._mbstrlenand_mbstrlen_l tests for multibyte-character validity and recognizes multibyte-character sequencessetlocale, _wsetlocale.If the string passed to _mbstrlen or_mbstrlen_l contain an invalid multibyte character for the code page, it returns -1 and sets errno to EILSEQ.

O valor de saída é afetado pela configuração do LC_CTYPE categoria de configuração da localidade; consulte setlocale para obter mais informações.As versões dessas funções sem o _l sufixo use a localidade corrente para esse comportamento dependente de localidade; as versões com o _l sufixo são idênticas exceto que usarem o parâmetro de localidade passado em vez disso. For more information, see Localidade.

Requisitos

Rotina

Cabeçalho necessário

strlen

<string.h>

strlen_l

<string.h>

wcslen, wcslen_l

<string.h> ou <wchar.h>

_mbslen, _mbslen_l

<mbstring.h>

_mbstrlen, _mbstrlen_l

<stdlib.h>

Para obter informações adicionais compatibilidade, consulte Compatibilidade na introdução.

Exemplo

// crt_strlen.c
// Determine the length of a string. For the multi-byte character
// example to work correctly, the Japanese language support for
// non-Unicode programs must be enabled by the operating system.


#include <string.h>
#include <locale.h>

int main()
{
   char* str1 = "Count.";
   wchar_t* wstr1 = L"Count.";
   char * mbstr1;
   char * locale_string;

   // strlen gives the length of single-byte character string
   printf("Length of '%s' : %d\n", str1, strlen(str1) );

   // wstrlen gives the length of a wide character string
   wprintf(L"Length of '%s' : %d\n", wstr1, wcslen(wstr1) );

   // A multibyte string: [A] [B] [C] [katakana A] [D] [\0]
   // in Code Page 932. For this example to work correctly,
   // the Japanese language support must be enabled by the
   // operating system.
   mbstr1 = "ABC" "\x83\x40" "D";

   locale_string = setlocale(LC_CTYPE, "Japanese_Japan");

   if (locale_string == NULL)
   {
      printf("Japanese locale not enabled. Exiting.\n");
      exit(1);
   }
   else
   {
      printf("Locale set to %s\n", locale_string);
   }

   // _mbslen will recognize the Japanese multibyte character if the
   // current locale used by the operating system is Japanese
   printf("Length of '%s' : %d\n", mbstr1, _mbslen(mbstr1) );

   // _mbstrlen will recognize the Japanese multibyte character
   // since the CRT locale is set to Japanese even if the OS locale
   // isnot. 
   printf("Length of '%s' : %d\n", mbstr1, _mbstrlen(mbstr1) );
   printf("Bytes in '%s' : %d\n", mbstr1, strlen(mbstr1) );   
  
}

Length of 'Count.' : 6 Length of 'Count.' : 6 Length of 'ABCァD' : 5 Length of 'ABCァD' : 5 Bytes in 'ABCァD' : 6

Equivalente do NET Framework

sistema::String::Length

Consulte também

Referência

Manipulação de seqüência de caracteres (CRT)

Interpretação de seqüências de caractere multibyte

Localidade

setlocale, _wsetlocale

strcat wcscat, _mbscat

strcmp wcscmp, _mbscmp

Funções strcoll

strcpy wcscpy, _mbscpy

strrchr, wcsrchr, _mbsrchr, _mbsrchr_l

_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l

strspn, wcsspn, _mbsspn, _mbsspn_l