查找宽字符串的长度

时间:2017-01-28 07:51:39

标签: widestring

要查找非宽字符串的长度,我们使用strlen函数:

#include <stdio.h>
#include <wchar.h>
#include <string.h>

int main(void)
{
char variable1[50] = "This is a test sent";
printf("The length of the string is: %d", strlen(variable1));
return 0;
}  
OUTPUT: 
The length of the string is: 18

我的书中所说的是使用语法:

wcslen(const wchar_t* ws)

但是,我已经尝试了很多方法,但它永远不会有效:

使用D格式规范:

#include <stdio.h>
#include <wchar.h>
int main(void)
{
wchar_t variable1[50] = "This is a test sent";
printf("The length of the string is: %d", wcslen(const wchar_t* variable1));
return 0;
}  

使用U格式指示器

#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t variable[50] = "This is a test sent";
printf("The length of the string is: %u", wcslen(variable1));
return 0;
}  

使用各种类型的SIZE_T

#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t variable1[50] = "This is a test sent";
size_t variable2 = wcslen(char wchar_t* variable1);
printf("The length of the string is: %u", variable2);
return 0;
}  

我们如何找到字符串的长度,以便得到类似于第一个的输出?(所有最后三个代码都会产生错误)

1 个答案:

答案 0 :(得分:0)

要启动宽字符串ypu,必须在字符串之前加上“ L”前缀:

wchar_t variable1[50] = L"This is a test sent";

这将使其成为“宽”字符串。

相关问题