C++获取含有中文的字符串长度 - 小众知识

C++获取含有中文的字符串长度

2022-12-10 05:55:42 苏内容
  标签: 字符串
阅读:1430

函数先放出来了:

  1. int lengthOfStr(string str)
  2. {
  3. int count = 0;
  4. for (int i = 0; str[i]; i++)
  5. {
  6. if (str[i] < 0) i++;
  7. //负数说明该字符为中文字符,占用两个字节,跳过后一个字节(i++),不进行统计
  8. count++;
  9. }
  10. return count;
  11. }

不想引用<string>头的可以用char数组:

  1. int lengthOfStr_c(char str[])
  2. {
  3. int count = 0;
  4. for (int i = 0; str[i]; i++)
  5. {
  6. if (str[i] < 0) i++;
  7. count++;
  8. }
  9. return count;
  10. }

----------------------------------

在Windows下,中文字符在C++中的内存占用为2字节,此时采用字符串长度获取函数得到的结果会将一个中文字符识别为两个长度:

  1. #include <stdio.h>
  2. #include <string>
  3. using namespace std;//string在std命名空间中
  4. int main()
  5. {
  6. string str = "abc中文def";
  7. printf("字符串为:%s\n", str.data());//data()函数回传字符串的指针,与c_str()相同
  8. int len;
  9. char str1[50];
  10. strcpy(str1, str.data());//赋值
  11. len = strlen(str1);
  12. printf("字符串的长度为(%d)\n", len);
  13. //使用strlen函数获取长度
  14. string str2 = str;//也可以用string str2.assign(str),这是string的赋值函数,不过和=没区别
  15. len = str2.length();//也可以用len = str2.size();
  16. printf("字符串的长度为(%d)\n", len);
  17. //使用string类的长度获取函数length()
  18. system("pause");
  19. }

输出:

  1. 字符串为:abc中文def
  2. 字符串的长度为(10)
  3. 字符串的长度为(10)
  4. 请按任意键继续. . .

而实际上,字符串的长度为8,并非上述方法的结果10。那么,如何获取真正的长度呢?

其实,我们不妨试试中文字符的值:

  1. char a = '中';
  2. char b = '文';
  3. char c = '字';
  4. char d = '符';
  5. printf("字符‘中’在编码中的值为%d\n",(int)a);
  6. printf("字符‘文’在编码中的值为%d\n",(int)b);
  7. printf("字符‘字’在编码中的值为%d\n",(int)c);
  8. printf("字符‘符’在编码中的值为%d\n",(int)d);
  9. system("pause");

输出:

  1. 字符‘中’在编码中的值为-48
  2. 字符‘文’在编码中的值为-60
  3. 字符‘字’在编码中的值为-42
  4. 字符‘符’在编码中的值为-5
  5. 请按任意键继续. . .

试试其他中文字符,也都是负数。

依据这一点,我们便可以做出一个获取含有中文的字符串长度的函数:

string版:

  1. int lengthOfStr(string str)
  2. {
  3. int count = 0;
  4. for (int i = 0; str[i]; i++)
  5. {
  6. if (str[i] < 0) i++;
  7. //负数说明该字符为中文字符,占用两个字节,跳过后一个字节(i++),不进行统计
  8. count++;
  9. }
  10. return count;
  11. }

char版: 虽然char数组也可以传入上面的函数,不过为了避免某些奇葩编译器,还是再写了一个函数,即拷即用:

  1. int lengthOfStr_c(char str[])
  2. {
  3. int count = 0;
  4. for (int i = 0; str[i]; i++)
  5. {
  6. if (str[i] < 0) i++;
  7. count++;
  8. }
  9. return count;
  10. }

不过,char版不可以传string。 

用前面的示例验证:

  1. #include <stdio.h>
  2. #include <string>
  3. using namespace std;
  4. int lengthOfStr(string str)
  5. {
  6. int count = 0;
  7. for (int i = 0; str[i]; i++)
  8. {
  9. if (str[i] < 0) i++;
  10. //负数说明该字符为中文字符,占用两个字节,跳过后一个字节(i++),不进行统计
  11. count++;
  12. }
  13. return count;
  14. }
  15. int lengthOfStr_c(char str[])
  16. {
  17. int count = 0;
  18. for (int i = 0; str[i]; i++)
  19. {
  20. if (str[i] < 0) i++;
  21. count++;
  22. }
  23. return count;
  24. }
  25. int main()
  26. {
  27. string str = "abc中文def";
  28. printf("字符串为:%s\n", str.data());//data()函数回传字符串的指针,与c_str()相同
  29. int len;
  30. char str1[50];
  31. strcpy(str1, str.data());//赋值
  32. len = strlen(str1);
  33. printf("字符串的长度为(%d)\n", len);
  34. //使用strlen函数获取长度
  35. len = lengthOfStr_c(str1);//len = lengthOfStr(str1);
  36. printf("字符串的长度为[%d]\n", len);
  37. //用上面的函数获取含有中文字符的字符串的真正长度
  38. string str2 = str;//也可以用string str2.assign(str),这是string的赋值函数,不过和=没区别
  39. len = str2.length();//也可以用len = str2.size();
  40. printf("字符串的长度为(%d)\n", len);
  41. //使用string类的长度获取函数length()
  42. len = lengthOfStr(str2);
  43. printf("字符串的长度为[%d]\n", len);
  44. //用上面的函数获取含有中文字符的字符串的真正长度
  45. system("pause");
  46. }

输出:

  1. 字符串为:abc中文def
  2. 字符串的长度为(10)
  3. 字符串的长度为[8]
  4. 字符串的长度为(10)
  5. 字符串的长度为[8]
  6. 请按任意键继续. . .

这个函数也可以获取没有中文字符的字符串长度:

  1. #include <stdio.h>
  2. #include <string>
  3. using namespace std;
  4. int lengthOfStr(string str)
  5. {
  6. int count = 0;
  7. for (int i = 0; str[i]; i++)
  8. {
  9. if (str[i] < 0) i++;
  10. //负数说明该字符为中文字符,占用两个字节,跳过后一个字节(i++),不进行统计
  11. count++;
  12. }
  13. return count;
  14. }
  15. int lengthOfStr_c(char str[])
  16. {
  17. int count = 0;
  18. for (int i = 0; str[i]; i++)
  19. {
  20. if (str[i] < 0) i++;
  21. count++;
  22. }
  23. return count;
  24. }
  25. int main()
  26. {
  27. string str = "abcdef";
  28. printf("字符串为:%s\n", str.data());//data()函数回传字符串的指针,与c_str()相同
  29. int len;
  30. char str1[50];
  31. strcpy(str1, str.data());//赋值
  32. len = strlen(str1);
  33. printf("字符串的长度为(%d)\n", len);
  34. //使用strlen函数获取长度
  35. len = lengthOfStr_c(str1);//len = lengthOfStr(str1);
  36. printf("字符串的长度为[%d]\n", len);
  37. //用上面的函数获取含有中文字符的字符串的真正长度
  38. string str2 = str;//也可以用string str2.assign(str),这是string的赋值函数,不过和=没区别
  39. len = str2.length();//也可以用len = str2.size();
  40. printf("字符串的长度为(%d)\n", len);
  41. //使用string类的长度获取函数length()
  42. len = lengthOfStr(str2);
  43. printf("字符串的长度为[%d]\n", len);
  44. //用上面的函数获取含有中文字符的字符串的真正长度
  45. system("pause");
  46. }

输出:

  1. 字符串为:abcdef
  2. 字符串的长度为(6)
  3. 字符串的长度为[6]
  4. 字符串的长度为(6)
  5. 字符串的长度为[6]
  6. 请按任意键继续. . .

  1. boost::regex rg("^[a-zA-Z0-9_\u4e00-\u9fa5]+$");
  2. boost::smatch sm;
  3. if(boost::regex_match( name, sm, rg )){
  4. return true;
  5. }
  6. else{
  7. return false;
  8. }
验证账号格式。

中文长度测试:

  1. int main()
  2. {
  3. setlocale(LC_ALL,"zh_CN.UTF-8");
  4. string string1("中文长度测试acdef");
  5. wchar_t str[256];
  6. int read = mbstowcs(NULL, string1.c_str(), string1.length());
  7. std::cout << "strign1.length = " <<  read << "\n";
  8. return 0;
  9. }







扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1