#include <iostream>
#include <string>
using namespace std;
void main()
{
string s;
s="-ZhangSan-99-LiSi-80-WangWu-85-ZhaoLiu-76-";
//单串数据查询浏览
string::size_type m1,m2,m3,pos=0;
string name,score;
while(true)
{
if(s.find("-",pos)!=string::npos)
{
m1=s.find("-",pos);
}
else
break;
if(s.find("-",m1+1)!=string::npos)
{
m2=s.find("-",m1+1);
}
else
break;
if(s.find("-",m2+1)!=string::npos)
{
m3=s.find("-",m2+1);
}
else
break;
name=s.substr(m1+1,m2-m1-1);
score=s.substr(m2+1,m3-m2-1);
cout<<"name:"<<name<<endl;
cout<<"score:"<<score<<endl;
pos=m3;
}
}
运行结果
基本的字符串处理加上文本文件的写入和读取可以实现数据本地化储存。
不过为了方便读取和筛选有用的信息,存储时需要按照一定的格式写入文本中。
常用操作:
赋值:”=”
连接:”+”
求长度: “.size()”方法
插入子串:s.insert(插入位置,串);
删除子串:s.erase(开始位置,长度);
查找子串:s.find(内容,开始位置);查找不到返回“string::npos”
取子串:s.substr(开始位置,长度);
替换子串:s.replace(开始位置,替换长度,替换串);