函数声明 | 函数功能 |
---|---|
| 用于复制宽字符字符串 |
| 用于比较两个宽字符字符串的大小写不敏感的差异 |
| 用于比较两个宽字符字符串的大小写不敏感的差异, 并考虑当前本地环境的语言和排序规则 |
| 用于将宽字符字符串转换为小写字母形式 |
| 用于在宽字符字符串中查找指定字符集中任意一个字符第一次出现的位置 |
| 用于比较两个宽字符字符串的前若干个字符的大小写不敏感的差异 |
| 用于将宽字符字符串中的前若干个字符设置为指定字符 |
| 用于将宽字符字符串反转 |
| 用于将宽字符字符串中的所有字符设置为指定字符 |
| 用于将宽字符串转换为长整形 |
| 用于将宽字符串转换为无符号长整型 |
| 用于将宽字符串转换为大写 |
| 用于创建字符转换描述符 |
| 用于将将源宽字符串的前 |
| 将源宽字符串中指定数量的字节复制到目标宽字符串中,即使目标内存和源内存重叠 |
函数声明 | 函数功能 |
---|---|
| 用于复制宽字符字符串 |
参数:
返回值:
NULL
。#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, world!"
const wchar_t *ws = L"Hello, world!";
// 复制宽字符字符串
wchar_t *ws_copy = wcsdup(ws);
if (ws_copy == NULL)
{
fprintf(stderr, "Error: Failed to allocate memory.\n");
return 1;
}
// 输出原始和副本字符串
wprintf(L"Original string: %ls\n", ws);
wprintf(L"Copy: %ls\n", ws_copy);
// 释放由 wcsdup() 函数分配的内存空间
free(ws_copy);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于比较两个宽字符字符串的大小写不敏感的差异 |
参数:
返回值:
s1
指向的字符串按字典顺序小于 s2
指向的字符串(忽略大小写),则函数返回一个负整数;s1
等于 s2
,则函数返回 0
;s1
指向的字符串按字典顺序大于 s2
指向的字符串(忽略大小写),则函数返回一个正整数。#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
const wchar_t *ws1 = L"Hello, huazie!";
// 宽字符字符串 "hello, HUAZIE!"
const wchar_t *ws2 = L"hello, HUAZIE!";
int cmp_result = wcsicmp(ws1, ws2);
// 输出比较结果
if (cmp_result < 0)
{
wprintf(L"%ls is less than %ls.\n", ws1, ws2);
}
else if (cmp_result == 0)
{
wprintf(L"%ls is equal to %ls.\n", ws1, ws2);
}
else
{
wprintf(L"%ls is greater than %ls.\n", ws1, ws2);
}
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于比较两个宽字符字符串的大小写不敏感的差异, 并考虑当前本地环境的语言和排序规则 |
参数:
返回值:
s1
指向的字符串按字典顺序小于 s2
指向的字符串(忽略大小写),则函数返回一个负整数;s1
等于 s2
,则函数返回 0
;s1
指向的字符串按字典顺序大于 s2
指向的字符串(忽略大小写),则函数返回一个正整数。#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void)
{
// 设置本地环境
setlocale(LC_ALL, "");
// 宽字符字符串 "Hello, world!"
const wchar_t *ws1 = L"Hello, huazie!";
// 宽字符字符串 "hello, WORLD!"
const wchar_t *ws2 = L"hello, HUAZIE!";
int cmp_result = wcsicoll(ws1, ws2);
// 输出比较结果
if (cmp_result < 0)
{
wprintf(L"%ls is less than %ls.\n", ws1, ws2);
}
else if (cmp_result == 0)
{
wprintf(L"%ls is equal to %ls.\n", ws1, ws2);
}
else
{
wprintf(L"%ls is greater than %ls.\n", ws1, ws2);
}
return 0;
}
注意: 在使用 wcsicoll()
函数前,需要先调用 setlocale()
函数设置本地环境。
函数声明 | 函数功能 |
---|---|
| 用于将宽字符字符串转换为小写字母形式 |
参数:
#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, HUAZIE!"
wchar_t ws[] = L"Hello, HUAZIE!";
// 将字符串转换为小写字母形式
wcslwr(ws);
// 输出转换后的字符串
wprintf(L"%ls\n", ws);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于在宽字符字符串中查找指定字符集中任意一个字符第一次出现的位置 |
参数:
wcspbrk()
函数会将 str
指向的宽字符字符串中的每个字符与 charset
指向的宽字符集合中的字符进行比较,直到找到其中任意一个相同的字符为止。
返回值:
NULL
。#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
const wchar_t *ws = L"Hello, huazie!";
// 查找第一个出现在 "abcd" 中的字符
wchar_t *result = wcspbrk(ws, L"abcd");
if (result == NULL)
{
wprintf(L"No matching character found.\n");
}
else
{
wprintf(L"First matching character: %lc\n", *result);
}
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于比较两个宽字符字符串的前若干个字符的大小写不敏感的差异 |
参数:
返回值:
s1
指向的字符串按字典顺序小于 s2
指向的字符串(忽略大小写),则函数返回一个负整数;s1
等于 s2
,则函数返回 0
;s1
指向的字符串按字典顺序大于 s2
指向的字符串(忽略大小写),则函数返回一个正整数。#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
const wchar_t *ws1 = L"Hello, huazie!";
// 宽字符字符串 "HELLO, HUAZIE!"
const wchar_t *ws2 = L"HELLO, HUAZIE!";
int cmp_result = wcsnicmp(ws1, ws2, 5);
// 输出比较结果
if (cmp_result < 0)
{
wprintf(L"%ls is less than %ls.\n", ws1, ws2);
}
else if (cmp_result == 0)
{
wprintf(L"%ls is equal to %ls.\n", ws1, ws2);
}
else
{
wprintf(L"%ls is greater than %ls.\n", ws1, ws2);
}
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于将宽字符字符串中的前若干个字符设置为指定字符 |
参数:
#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
wchar_t ws[] = L"Hello, huazie!";
// 将前五个字符设置为 '*'
wcsnset(ws, L'*', 5);
// 输出修改后的字符串
wprintf(L"%ls\n", ws);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于将宽字符字符串反转 |
参数:
#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
wchar_t ws[] = L"Hello, huazie!";
// 反转字符串
wcsrev(ws);
// 输出反转后的字符串
wprintf(L"%ls\n", ws);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于将宽字符字符串中的所有字符设置为指定字符 |
参数:
#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
wchar_t ws[] = L"Hello, huazie!";
// 输出修改前的字符串
wprintf(L"Before: %ls\n", ws);
// 将字符串中的每个字符都设置为 '#'
wcsset(ws, L'#');
// 输出修改后的字符串
wprintf(L"After : %ls\n", ws);
return 0;
}
注意: wcsset()
函数会修改原始字符串,因此需要在操作前确保原始字符串可以被修改。
函数声明 | 函数功能 |
---|---|
| 用于将宽字符串转换为长整形 |
参数:
nullptr
时,不会返回无法被识别的宽字符位置#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main()
{
const wchar_t* str = L"123456789";
//const wchar_t* str = L"12345a6789";
//const wchar_t* str = L"a123456789";
wchar_t* endptr;
long int num;
num = wcstoll(str, &endptr, 10);
wprintf(L"The number is %ld\n", num);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于将宽字符串转换为无符号长整型 |
参数:
nullptr
时,不会返回无法被识别的宽字符位置#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main()
{
const wchar_t* str = L"123456789";
//const wchar_t* str = L"12345a6789";
//const wchar_t* str = L"a123456789";
wchar_t* endptr;
unsigned long long int num;
num = wcstoull(str, &endptr, 10);
wprintf(L"The number is %llu\n", num);
return 0;
}
wcstoull()
函数的用法和 wcstoll()
函数类似,不再赘述了。
函数声明 | 函数功能 |
---|---|
| 用于将宽字符串转换为大写 |
参数:
#include <stdio.h>
#include <wchar.h>
int main(void)
{
// 宽字符字符串 "Hello, huazie!"
wchar_t ws[] = L"Hello, huazie!";
// 将宽字符串转换为大写字母形式
wcsupr(ws);
// 输出转换后的字符串
wprintf(L"%ls\n", ws);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于创建字符转换描述符 |
| 通过 |
wctrans 参数:
towctrans 参数:
#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
int main() {
// 创建一个转换描述符,用于将小写字母转换为大写字母
wctrans_t to_upper = wctrans("toupper");
// 使用转换描述符将 wchar_t 类型字符串中的所有小写字母转换为大写字母
wchar_t str[] = L"hello huazie";
for (int i = 0; str[i] != L'\0'; i++) {
str[i] = towctrans(str[i], to_upper);
}
// 输出结果:"HELLO WORLD"
wprintf(L"%ls\n", str);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 用于将将源宽字符串的前 |
参数:
#include <stdio.h>
#include <wchar.h>
#include <string.h>
int main()
{
const wchar_t* src = L"Hello, Huazie!";
wchar_t dest[20];
wprintf(L"Original string: %ls\n", src);
// 将源字符串内容拷贝到目标字符串中
wchar_t* ptr = wmempcpy(dest, src, wcslen(src) * sizeof(wchar_t));
*ptr = L'\0';
wprintf(L"Copied string: %ls\n", dest);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 将源宽字符串中指定数量的字节复制到目标宽字符串中,即使目标内存和源内存重叠 |
参数:
#include <stdio.h>
#include <wchar.h>
#include <string.h>
int main()
{
wchar_t str[30] = L"Hello, Huazie!";
wprintf(L"Original string: %ls\n", str);
// 将字符串中前5个字符移动到后面
wmemmove(str + 6, str, 5 * sizeof(wchar_t));
wprintf(L"Moved string: %ls\n", str);
return 0;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。