_strnset、_strnset_l、_wcsnset、_wcsnset_l、_mbsnset、_mbsnset_l_strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l

将字符串的字符初始化为给定字符。Initializes characters of a string to a given character. 这些函数的更安全版本已发布;请参阅 _strnset_s、_strnset_s_l、_wcsnset_s、_wcsnset_s_l、_mbsnset_s、_mbsnset_s_l More secure versions of these functions exist; see _strnset_s, _strnset_s_l, _wcsnset_s, _wcsnset_s_l, _mbsnset_s, _mbsnset_s_l.


_mbsnset_mbsnset_l 无法用于在 Windows 运行时中执行的应用程序。_mbsnset and _mbsnset_l cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


char *_strnset(  
   char *str,  
   int c,  
   size_t count   
char *_strnset_l(  
   char *str,  
   int c,  
   size_t count,  
   locale_t locale  
wchar_t *_wcsnset(  
   wchar_t *str,  
   wchar_t c,  
   size_t count   
wchar_t *_wcsnset_l(  
   wchar_t *str,  
   wchar_t c,  
   size_t count,  
   _locale_t locale  
unsigned char *_mbsnset(  
   unsigned char *str,  
   unsigned int c,  
   size_t count   
unsigned char *_mbsnset_l(  
   unsigned char *str,  
   unsigned int c,  
   size_t count,  
   _locale_t locale  


要修改的字符串。String to be altered.

字符设置。Character setting.

要设置的字符数。Number of characters to be set.

要使用的区域设置。Locale to use.

返回值Return Value

返回指向修改后的字符串的指针。Returns a pointer to the altered string.


_strnset 函数最多可以将 str 的前 count 个字符设置为 c(已转换为 char)。The _strnset function sets, at most, the first count characters of str to c (converted to char). 如果 count 大于 str 的长度,则会使用 str 的长度而使用 countIf count is greater than the length of str, the length of str is used instead of count.

_wcsnset_mbsnset 分别是 _strnset 的宽字符及多字节字符版本。_wcsnset and _mbsnset are wide-character and multibyte-character versions of _strnset. _wcsnset 的字符串参数和返回值是宽字符字符串;_mbsnset 的是多字节字符字符串。The string arguments and return value of _wcsnset are wide-character strings; those of _mbsnset are multibyte-character strings. 否则这三个函数否则具有相同行为。These three functions behave identically otherwise.

_mbsnset 验证其参数;如果 str 为空指针,则调用无效的参数处理程序,如参数验证中所述。_mbsnset validates its parameters; if str is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation . 如果允许执行继续,则 _mbsnset 返回 NULL,并将 errno 设置为 EINVALIf execution is allowed to continue, _mbsnset returns NULL and sets errno to EINVAL. _strnset_wcsnset 不会验证其参数。_strnset and _wcsnset do not validate their parameters.

输出值受区域设置的 LC_CTYPE 类别设置影响;有关详细信息,请参阅 setlocaleThe output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale for more information. 这些不带 _l 后缀的函数的版本使用为该区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. 有关详细信息,请参阅 LocaleFor more information, see Locale.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcsnset _strnset _mbsnbset _wcsnset
_tcsnset_l _strnset_l _mbsnbset_l _wcsnset_l


例程所返回的值Routine 必需的标头Required header
_strnset <string.h><string.h>
_strnset_l <tchar.h><tchar.h>
_wcsnset <string.h> 或 <wchar.h><string.h> or <wchar.h>
_wcsnset_l <tchar.h><tchar.h>
_mbsnset, _mbsnset_l_mbsnset, _mbsnset_l <mbstring.h><mbstring.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_strnset.c  
// compile with: /W3  
#include <string.h>  
#include <stdio.h>  

int main( void )  
   char string[15] = "This is a test";  
   /* Set not more than 4 characters of string to be *'s */  
   printf( "Before: %s\n", string );  
   _strnset( string, '*', 4 ); // C4996  
   // Note: _strnset is deprecated; consider using _strnset_s  
   printf( "After:  %s\n", string );  
Before: This is a test  
After:  **** is a test  

请参阅See Also

字符串操作 String Manipulation
区域设置 Locale
多字节字符序列的解释 Interpretation of Multibyte-Character Sequences
strcat、wcscat、_mbscat strcat, wcscat, _mbscat
strcmp、wcscmp、_mbscmp strcmp, wcscmp, _mbscmp
strcpy、wcscpy、_mbscpy strcpy, wcscpy, _mbscpy
_strset、_strset_l、_wcsset、_wcsset_l、_mbsset、_mbsset_l_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l