看流星社区

 找回密码
 注册账号
查看: 2144|回复: 1

VC++如何从中文字符串数组中输出一个汉字

[复制链接]

该用户从未签到

发表于 2014-7-27 08:53:24 | 显示全部楼层 |阅读模式
例如
a[]="点点滴滴";
输出“点”字
我用的是VC++ 6.0

该用户从未签到

发表于 2014-7-27 08:53:41 | 显示全部楼层
#include<iostream>
#include<string>

//取得字符串字符个数,不限中英文
unsigned int GetStringCount(const std::string &s)
{
short ch;
unsigned cnt = s.size();
for(size_t i=0; i<s.size(); ++i)
{
  ch = s.at(i);
  if(ch&0x1000) ++i,--cnt;
}
return cnt;
}

//取得以0为起始索引的指定位置字符
std::string GetString(std::string &s, size_t index)
{
short ch;
std::string res;
unsigned cnt = index;
for(size_t i=0; i<s.size() && i<cnt; ++i)
{
  ch = s.at(i);
  if(ch&0x1000) ++i,++cnt;
}
ch = s.at(i);
if(ch&0x1000) res=s.substr(i,2);
else res=s.substr(i,1);
return res;
}

int main()
{
std::string s="点点滴滴";
std::cout<<GetStringCount(s)<<std::endl;
std::cout<<GetString(s,2)<<std::endl;
return EXIT_SUCCESS;
}
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-5-19 23:10

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表