string类的应用

baigoo 2014-01-18 12:26:46
// String.cpp : Defines the entry point for the console application.
// description: string class usage.

#include "stdafx.h"
#include <string>
#include <iostream>
//#define TEST

using std::cout;
using std::cin;
using std::endl;
using std::string;

//define class Editor.
/***********************************************************************************/

class Editor
{
public:
Editor():current_pos(0),next_pos(0),strContent(""){}
Editor(const string& strData):current_pos(0),next_pos(0),strContent(strData){}
~Editor(){}

int Find(const string& strData);
int Insert(string::size_type npos,const string &strData);
int Replace(const string& strSource,const string& strTarget);
int Remove(const string& strData);
void Move_pos_to_first();
void Print();

private:
string::size_type current_pos;
string::size_type next_pos;
string strContent;
};

//Find() define
int Editor::Find(const string& strData)
{
current_pos = strContent.find(strData,next_pos);
if(current_pos != string::npos)
{
cout << "Find " << strData << " at postion:" << current_pos << endl;
next_pos = current_pos + strData.length();
}
else
{
cout << "Error:Can't find " << strData << endl;
next_pos = current_pos = 0;
}
return current_pos;
}

//Insert() define
int Editor::Insert(string::size_type npos,const string& strData)
{
strContent.insert(npos, strData);
return 0;
}

//Replace() define
int Editor::Replace(const string& strSource,const string& strTarget)
{
current_pos = strContent.find(strSource, next_pos);
if(current_pos != string::npos)
{
strContent.replace(current_pos, strSource.length(), strTarget);
next_pos = current_pos + strSource.length();
}
else
{
cout << "Error:Can't find " << strSource << endl;
next_pos = current_pos = 0;
}
return 0;
}

//Remove() define
int Editor::Remove(const string& strData)
{
current_pos = strContent.find(strData, next_pos);
if(current_pos != string::npos)
{
strContent.erase(current_pos, strData.length());
next_pos = current_pos + strData.length();
}
else
{
cout << "Error:Can't find " << strData << endl;
next_pos = current_pos = 0;
}
return 0;
}

//Move_pos_to_first() define
void Editor::Move_pos_to_first()
{
current_pos = next_pos = 0;
}

//Print() define
void Editor::Print()
{
cout << strContent << endl;
}

/***********************************************************************************/

//main
int _tmain(int argc, _TCHAR* argv[])
{
//test
#ifdef TEST
string strWebsite;
strWebsite = "www.baidu.com";
cout << "strWebsite = " << strWebsite << endl;

//string assignment
string cpyWebsite = strWebsite;
cout << "cpyWebsite = " << cpyWebsite << endl;

cout << "strContent:" << endl;
string strContent = "www.baidu.com,this is a c++ program and we use STL string class!";
cout << strContent << endl;

//string :find()
int pos = strContent.find("baidu",0);
if(pos != strContent.npos)
{
cout << "Find \"baidu\" at position:" << pos << endl;
}
else
{
cout << "Can't find \"baidu\"!" << endl;
}

//string :erase()
strContent.erase(4, 5);
cout << "After erase:" << endl;
cout << strContent << endl;

//string :insert()
strContent.insert(4, "google");
cout << "After insert:" << endl;
cout << strContent << endl;

//string :replace()
strContent.replace(4, 6, "hao123");
cout << "After replace:" << endl;
cout << strContent << endl;
cout << "strlen(strContent) = " << strContent.length() + 1 << endl;
cout << "strContent.npos = " << strContent.npos << endl;
cout << "string::npos = " << string::npos << endl;
#endif

//define an object o_Editor belongs to class Editor.
Editor o_Editor = "www.baidu.com is a website.This is a c++ program and\
we use STL string.baidu is a search engine in China.";

//在字符串连续寻找一串字符,当找完整个字符串都找不到时,会再次从头开始寻找
for(int i = 0;i < 5;i ++)
{
o_Editor.Find("baidu");
}

//把位置置零
o_Editor.Move_pos_to_first();
o_Editor.Print();

//删除长串字符串中的"baidu"
for(int i = 0;i < 3;i ++)
{
o_Editor.Remove("baidu");
}
o_Editor.Print();

//插入一个字符串"google"
o_Editor.Move_pos_to_first();
o_Editor.Insert(4, "google");
o_Editor.Insert(78, "google");
o_Editor.Print();

//替换一个字符串
o_Editor.Move_pos_to_first();
for(int i = 0;i < 3;i ++)
{
o_Editor.Replace("google", "hao123");
}
o_Editor.Print();
o_Editor.Move_pos_to_first();

return 0;
}

...全文
180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
零度的折翅 2014-01-19
  • 打赏
  • 举报
回复
引用 3 楼 chenyan2010521 的回复:
不是,学习笔记,非技术区发的贴!
原来是这样的呀!!!
lm_whales 2014-01-19
  • 打赏
  • 举报
回复
不错。。。。。。。。。
我系麦叔叔 2014-01-18
  • 打赏
  • 举报
回复
分享技术贴?
baigoo 2014-01-18
  • 打赏
  • 举报
回复
不是,学习笔记,非技术区发的贴!
allenltiverson 2014-01-18
  • 打赏
  • 举报
回复
这是想说明什么?

15,446

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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