C++中string类的中文问题

ufouser 2005-09-15 04:54:56
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "一二三四年";
cout << str.length() << endl
<< str.substr(0,2) << endl
<< str.substr(2,4) << endl
<< str.substr(4,6) << endl
<< str.substr(6,8) << endl
<< str.substr(8,10) << endl;
return 0;
}

想一个一个汉字输出的, 结果老是输出几个
...全文
802 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ufouser 2005-09-15
  • 打赏
  • 举报
回复
哈哈 我晕 多谢各位
foochow 2005-09-15
  • 打赏
  • 举报
回复
//楼主按区间来做,要这样..
#include<iterator>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "一二三四年";
string::iterator it=str.begin();
cout << str.length() << endl
<< string(it+0,it+2) << endl
<< string(it+2,it+4) << endl
<< string(it+4,it+6) << endl
<< string(it+6,it+8) << endl
<< string(it+8,it+10) << endl;
return 0;
}
foochow 2005-09-15
  • 打赏
  • 举报
回复
呵呵,楼主搞错substr的用法了
leifen 2005-09-15
  • 打赏
  • 举报
回复
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "Ò»¶þÈýËÄÄê";

cout << str.length() << endl
<< str.substr(0,2) << endl
<< str.substr(2,2) << endl
<< str.substr(4,2) << endl
<< str.substr(6,2) << endl
<< str.substr(8,2) << endl;
return 0;
}
healer_kx 2005-09-15
  • 打赏
  • 举报
回复
前面就不该说那些没有用的, 晚了...
qhfu 2005-09-15
  • 打赏
  • 举报
回复
basic_string substr(
size_type _Off = 0,
size_type _Count = npos//The number of characters that are to be copied if they are present.
) const;
healer_kx 2005-09-15
  • 打赏
  • 举报
回复
看你的系统的默认编码方式,
中文系统一般是GBK-GB2312的,英文一般是ISO_8859_1
;
GBK是2个字节一个汉字,/

哦,你问的不是这个啊. ... ...

大哥,substr不是你那么用的哦, 这不是VB, 是第一个,取几个

<< str.substr(0,2) << endl
<< str.substr(2,2) << endl
<< str.substr(4,2) << endl
<< str.substr(6,2) << endl
<< str.substr(8,2) << endl;


qhfu 2005-09-15
  • 打赏
  • 举报
回复
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "一二三四年";
cout << str.length() << endl
<< str.substr(0,2) << endl
<< str.substr(2,2) << endl
<< str.substr(4,2) << endl
<< str.substr(6,2) << endl
<< str.substr(8,2) << endl;
return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧