

函数声明 | 函数功能 |
|---|---|
| 用于将一个宽字符字符串追加到另一个宽字符字符串的末尾 |
| 用于在宽字符串中查找指定字符的位置 |
| 用于比较两个宽字符串的大小;它将两个字符串逐个字符进行比较,直到遇到不同的字符或者其中一个字符串结束为止 |
| 用于比较两个宽字符串的大小 |
| 用于将一个宽字符串复制到另一个字符串中 |
| 用于将日期和时间格式化为宽字符字符串 |
| 用于计算宽字符串的长度 |
| 用于将一个宽字符串的一部分追加到另一个宽字符串末尾 |
| 用于比较两个宽字符串的前若干个字符是否相同 |
| 用于将一个宽字符串的一部分复制到另一个宽字符串中 |
| 用于将宽字符串转换为多字节字符串 |
| 用于在一个宽字符串中查找另一个宽字符串 |
| 用于查找宽字符串中连续包含某些字符集合中的字符的最长前缀 |
| 用于将宽字符串转换为双精度浮点数 |
函数声明 | 函数功能 |
|---|---|
| 用于将一个宽字符字符串追加到另一个宽字符字符串的末尾 |
参数:
#include <stdio.h>
#include <wchar.h>
int main() {
wchar_t dest[30] = L"Hello";
const wchar_t *src = L", Huazie!";
wcscat(dest, src);
wprintf(L"%ls\n", dest);
return 0;
}在上面的示例代码中,
30 的 wchar_t 数组 dest,并初始化为 "Hello";src,指向字符串 ", Huazie!";wcscat() 函数将 src 字符串中的所有字符追加到 dest 字符串的末尾,形成新的宽字符字符串 "Hello, Huazie!";wprintf() 函数将新的字符串输出到控制台。注意: 在使用 wcscat() 函数时,需要确保目标字符串 dest 的空间足够大,以容纳源字符串 src 的所有字符和一个结束符(\0)。如果目标字符串的空间不足,可能会导致数据覆盖和未定义行为。

函数声明 | 函数功能 |
|---|---|
| 用于在宽字符串中查找指定字符的位置 |
参数:
返回值:
str 字符串中查找到第一个与 wc 相等的宽字符,则返回该字符在字符串中的地址;#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *str = L"hello, huazie";
wchar_t c = L'u';
wchar_t *p = wcschr(str, c);
if (p != NULL)
printf("Found %lc at position %d.\n", c, p - str);
else
printf("%lc not found.\n", c);
return 0;
}在上面的示例代码中,
str,并初始化为 "hello, huazie";c,值为 'u';wcschr() 函数在 str 字符串中查找字符 c,并将返回结果保存在指针变量 p 中;
函数声明 | 函数功能 |
|---|---|
| 用于比较两个宽字符串的大小;它将两个字符串逐个字符进行比较,直到遇到不同的字符或者其中一个字符串结束为止 |
参数:
返回值:
str1 小于 str2,则返回一个负整数;str1 等于 str2,则返回 0;str1 大于 str2,则返回一个正整数。#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *str1 = L"hello";
const wchar_t *str2 = L"huazie";
int result = wcscmp(str1, str2);
if (result < 0)
printf("%ls is less than %ls.\n", str1, str2);
else if (result == 0)
printf("%ls is equal to %ls.\n", str1, str2);
else
printf("%ls is greater than %ls.\n", str1, str2);
return 0;
}在上述的示例代码中,
str1 和 str2,分别初始化为 "hello" 和 "huazie";wcscmp() 函数比较两个字符串的大小,并将返回结果保存在变量 result 中;result 的值,输出相应的比较结果;
函数声明 | 函数功能 |
|---|---|
| 用于比较两个宽字符串的大小 |
参数:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, ""); // 设置本地化环境
const wchar_t *str1 = L"hello";
const wchar_t *str2 = L"huazie";
int result = wcscoll(str1, str2);
if (result < 0)
printf("%ls is less than %ls.\n", str1, str2);
else if (result == 0)
printf("%ls is equal to %ls.\n", str1, str2);
else
printf("%ls is greater than %ls.\n", str1, str2);
return 0;
}在上述的示例代码中,
setlocale() 函数设置本地化环境为当前系统默认设置;str1 和 str2,分别初始化为 "hello" 和 "huazie";wcscoll() 函数比较两个字符串的大小,并将返回结果保存在变量 result 中。result 的值,输出相应的比较结果。注意: 在使用 wcscoll() 函数比较宽字符串大小时,需要确保本地化环境正确设置,以便该函数能够正常工作。如果没有设置本地化环境或者设置错误,可能会导致比较结果不准确。

函数声明 | 函数功能 |
|---|---|
| 用于将一个宽字符串复制到另一个字符串中 |
参数:
#include <stdio.h>
#include <wchar.h>
int main()
{
wchar_t dest[20];
const wchar_t *src = L"Hello, huazie!";
wcscpy(dest, src);
wprintf(L"%ls\n", dest);
return 0;
}在上述的示例代码中,
20 的 wchar_t 数组 dest;src,指向字符串 "Hello, huazie!";wcscpy() 函数将 src 字符串中的所有字符复制到 dest 字符串中,形成新的宽字符字符串 dest;wprintf() 函数将新的字符串输出到控制台。
函数声明 | 函数功能 |
|---|---|
| 用于将日期和时间格式化为宽字符字符串 |
参数:
#include <stdio.h>
#include <wchar.h>
#include <time.h>
int main()
{
time_t current_time;
struct tm *time_info;
wchar_t buffer[80];
time(¤t_time);
time_info = localtime(¤t_time);
wcsftime(buffer, 80, L"%c", time_info);
wprintf(L"The current date and time is: %ls\n", buffer);
return 0;
}在上面的示例代码中,
current_time 来存储当前时间,以及一个指向 struct tm 类型的指针 time_info;time() 函数获取当前时间,并使用 localtime() 函数将时间转换为本地时间,存储在 time_info 指针变量中;wcsftime() 函数将日期和时间格式化为宽字符字符串,并存储到缓冲区 buffer 中;wprintf() 函数输出格式化后的字符串。
函数声明 | 函数功能 |
|---|---|
| 用于计算宽字符串的长度 |
参数:
#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *str = L"Hello, huazie!";
size_t len = wcslen(str);
wprintf(L"The length of '%ls' is %zu.\n", str, len);
return 0;
}在上面的示例代码中,
str,指向字符串 "Hello, huazie!";wcslen() 函数计算 str 字符串的长度,并将结果保存在变量 len 中;wprintf() 函数输出字符串的长度。
函数声明 | 函数功能 |
|---|---|
| 用于将一个宽字符串的一部分追加到另一个宽字符串末尾 |
参数:
#include <stdio.h>
#include <wchar.h>
int main()
{
wchar_t dest[20] = L"Hello, ";
const wchar_t *src = L"huazie!";
size_t n = 3;
wcsncat(dest, src, n);
wprintf(L"%ls\n", dest);
return 0;
}在上述的示例代码中,
20 的 wchar_t 数组 dest,并初始化为 "Hello, ";src,指向字符串 "huazie!"。wcsncat() 函数将 src 字符串中的前 3 个字符追加到 dest 字符串的末尾,形成新的宽字符字符串 dest;wprintf() 函数将新的字符串输出到控制台。
函数声明 | 函数功能 |
|---|---|
| 用于比较两个宽字符串的前若干个字符是否相同 |
参数:
返回值:
将字符串 str1 和字符串 str2 中的前 n 个字符进行比较
0;str1 在前 n 个字符中的第一个不同于字符串 str2 对应字符的字符大于字符串 str2 对应字符的字符,返回值为正数;str1 在前 n 个字符中的第一个不同于字符串 str2 对应字符的字符小于字符串 str2 对应字符的字符,返回值为负数。#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *str1 = L"hello";
const wchar_t *str2 = L"huazie";
size_t n = 3;
int result = wcsncmp(str1, str2, n);
if (result < 0)
printf("%ls is less than %ls in the first %zu characters.\n", str1, str2, n);
else if (result == 0)
printf("%ls is equal to %ls in the first %zu characters.\n", str1, str2, n);
else
printf("%ls is greater than %ls in the first %zu characters.\n", str1, str2, n);
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 用于将一个宽字符串的一部分复制到另一个宽字符串中 |
参数:
#include <stdio.h>
#include <wchar.h>
int main()
{
wchar_t dest[20];
const wchar_t *src = L"Hello, huazie!";
size_t n = 5;
wcsncpy(dest, src, n);
dest[n] = L'\0';
wprintf(L"%ls\n", dest);
return 0;
}在上面的示例代码中,
20 的 wchar_t 数组 dest,以及一个指向常量宽字符串的指针 src,指向字符串 "Hello, huazie!";wcsncpy() 函数将 src 字符串中的前 5 个字符复制到 dest 字符串中,形成新的宽字符字符串 dest;\0 以确保字符串结束,并使用 wprintf() 函数将新的字符串输出到控制台。
函数声明 | 函数功能 |
|---|---|
| 用于将宽字符串转换为多字节字符串 |
参数:
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "");
const wchar_t *src = L"Hello, huazie!";
size_t n = wcslen(src) + 1;
char *dest = (char *) malloc(n * sizeof(char));
if (dest == NULL)
{
fprintf(stderr, "Memory allocation failed.\n");
return EXIT_FAILURE;
}
mbstate_t state = {0};
wcsrtombs(dest, &src, n, &state);
printf("%s\n", dest);
free(dest);
return 0;
}在上面的示例代码中,
setlocale() 函数设置程序的本地化环境,以便可以正确地进行宽字符和多字节字符之间的转换;src,指向字符串 "Hello, huazie!";dest,并初始化为 0;wcsrtombs() 函数将宽字符串 src 转换为多字节字符串,并存储到缓冲区 dest 中;printf() 函数输出多字节字符串,并释放目标缓存区的内存。注意: 在使用 wcsrtombs() 函数进行宽字符和多字节字符转换时,需要确保程序的本地化环境已经正确设置,否则可能会导致转换失败或者输出结果不正确。此外,在分配缓冲区 dest 的大小时,可以考虑将源字符串的长度加 1,以容纳字符串的结尾空字符(\0)。最后在使用完毕后要记得释放缓冲区的内存。

函数声明 | 函数功能 |
|---|---|
| 用于在一个宽字符串中查找另一个宽字符串 |
参数:
返回值:
haystack 字符串中查找到第一个匹配 needle 子串的位置,则返回指向匹配位置的指针;NULL)。#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *haystack = L"Hello, huazie!";
const wchar_t *needle = L"huazie";
wchar_t *result = wcsstr(haystack, needle);
if (result != NULL)
wprintf(L"The substring '%ls' was found at position %d.\n", needle, result - haystack);
else
wprintf(L"The substring '%ls' was not found in '%ls'.\n", needle, haystack);
return 0;
}在上述的示例代码中,
haystack,指向字符串 "Hello, huazie!"。needle,指向字符串 "huazie";wcsstr() 函数在 haystack 字符串中查找子串 needle,并将结果指针保存在变量 result 中。result 的值,输出相应的查找结果。注意: 在使用 wcsstr() 函数查找子串时,该函数会自动遍历整个字符串,直到找到匹配的子串或者结束字符串。如果要查找的子串在字符串中多次出现,该函数将返回第一次出现的位置,并不会考虑后续的匹配。

函数声明 | 函数功能 |
|---|---|
| 用于查找宽字符串中连续包含某些字符集合中的字符的最长前缀 |
参数:
#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *str = L"123456789a0";
const wchar_t *accept = L"0123456789";
size_t length = wcsspn(str, accept);
wprintf(L"The length of the prefix that contains digits is %zu.\n", length);
return 0;
}在上述的示例代码中,
str,指向字符串 "123456789a0";accept,指向字符串 "0123456789",表示数字字符的集合;wcsspn() 函数查找 str 字符串中连续包含数字字符集合中的字符的最长前缀,并将返回结果保存在变量 length 中。length 的值,调用 wprintf() 函数 输出 最长前缀的长度。注意: 在使用 wcsspn() 函数查找宽字符串中的字符集合时,该函数会自动遍历整个字符串,直到找到第一个不在字符集合中的字符或者结束字符串。如果要查找的字符集合为空串,则返回 0。

函数声明 | 函数功能 |
|---|---|
| 用于将宽字符串转换为双精度浮点数 |
参数:
返回值:
nptr 字符串中解析出一个双精度浮点数,则返回该数值;nptr 字符串不包含有效的浮点数,则返回 0;endptr 指针中。#include <stdio.h>
#include <wchar.h>
int main()
{
const wchar_t *str = L"3.14159265358979323846";
//const wchar_t *str = L"3.141592653589793a23846";
//const wchar_t *str = L"a3.14159265358979323846";
wchar_t *end;
double number = wcstod(str, &end);
if (end == str)
wprintf(L"No digits were found.\n");
else if (*end != L'\0')
wprintf(L"Invalid character at position %ld: '%lc'.\n", end - str, *end);
else
wprintf(L"The parsed number is %.20lf.\n", number);
return 0;
}在上面的示例代码中,
str,指向字符串 "3.14159265358979323846";wcstod() 函数将字符串转换为双精度浮点数,并将结果保存在变量 number 中。number 和 endptr 指针所指向的值,输出相应的转换结果。注意: 在使用 wcstod() 函数转换宽字符串为双精度浮点数时,要确保字符串中只包含有效的浮点数表示,否则可能会导致转换错误或者未定义行为。



原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。