#include <iostream>
using namespace std; int main()
{
//initilization
string str("abc.ddd");
const string cstr("fff.ccc");
string substr1(str, ); //c.ddd
string substr2(str, , ); //c.d
string substr3("abcdefg", ); //ab
cout << "the value of substr1 is: " << substr1 << endl;
cout << "the value of substr2 is: " << substr2 << endl;
cout << "the value of substr3 is: " << substr3 << endl; //compare
if(str > cstr)
cout << "abc.ddd is larger than fff.ccc." << endl;
else
cout << "abc.ddd is less than fff.ccc." << endl;
if(str.compare(cstr) > )
cout << "abc.ddd is larger than fff.ccc." << endl;
else
cout << "abc.ddd is less than fff.ccc." << endl; //assign
str = "sadfasdf";
str.assign("a",); //a,\0,\0,\0,\0
str.assign(,'a'); //a, a, a, a, a
cout << "the value of str is: " << str << endl; //aaaaa //append
str.append("bcd");
cout << "the value of s after append is: " << str << endl; //aaaaabcd //insert
//s.insert(1, 'd'); NOK!
str.insert(, "ddd"); //adddaaaabcd
cout << "the value of s after append is: " << str << endl; //find
string::size_type idx = str.find(".");
cout << "the index of . is: " << idx << endl; //3 //substring
string basestr = str.substr(, idx); //abc
string extestr = str.substr(idx + , string::npos); //ddd
cout << "the substr of str.substr(0, idx) is: " << basestr << endl;
cout << "the substr of str.substr(idx, string::npos) is: " << extestr << endl; //compare
if(extestr == "ddd")
cout << "ddd file is found!" << endl;
else
cout << "ddd file is not found!" << endl; //replace
string tmpname(str.replace(idx + , string::npos, "xxx")); //abc.xxx
cout << "the string after replace extention is: " << tmpname << endl; //erase
str = "internal";
str.replace(,,"ex"); //external
str.erase();
str.erase(,);
cout << "the string after erase is: " << str << endl; //clear
str.clear();
cout << "the string after clear is: " << str << endl;
if(str.empty())
cout << "the string is empty." << endl;
else
cout << "the string is not empty." << endl;
if(str.begin() == str.end())
cout << "equal." << endl;
else
cout << "unequal" << endl; //reverse
str = "abcd";
reverse(str.begin(), str.end());
cout << "the string after reverse is: " << str << endl;
str.assign(str.rbegin(), str.rend());
cout << "the string after reverse is: " << str << endl; //data
const char* pa = str.data(); //size(), length()
cout << "the size of str is: " << str.size() << endl;
cout << "the size of str is: " << str.length() << endl; //[], at()
char& r = str[];
char* p = &str[];
cout << "the 3rd char of str is: " << r << endl;
cout << "the 4rd char of str is: " << *p << endl;
str = "new value";
//reference is invalid after str is re-assigned
r = 'X';
cout << "The value of str is: " << str << endl; //advanced find
//Input: I was a deer
//Output: reed a saw I
const string delims(" \t,.;");
string line;
cout << "Please input a sentence: " << endl;
getline(cin, line,'\n');
cout << "The input sentence is: " << line << endl;
//while find a word
string::size_type begIdx, endIdx;
begIdx = line.find_first_not_of(delims);
while(begIdx != string::npos){
endIdx = line.find_first_of(delims, begIdx);
if(endIdx == string::npos);
//endIdx = line.length();
for(int i = endIdx - ; i >= static_cast<int>(begIdx); --i)
cout << line[i];
cout << ' ';
begIdx = line.find_first_not_of(delims, endIdx); }
cout << endl; }

C++string的操作的更多相关文章

  1. redis 的使用 (基础, key操作, string类型操作)

    使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...

  2. Redis系列-存储篇string主要操作函数小结

    通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...

  3. string的操作

    除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...

  4. Library string type(2)——关于String的操作

    关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...

  5. 022 StringTokenizer替换掉String的操作

    一:说明 1.说明 String的操作特别消耗内存,所以可以考虑优化. 二:程序 1.程序修改 这部分程序属于Mapper端的程序,稍微优化一下. 2.程序 //Mapper public stati ...

  6. string stack操作要注重细节问题

    A string S consisting of N characters is considered to be properly nested if any of the following co ...

  7. java String 的+操作导致的问题

    不说别的先看代码截图: 结果如下: 很好奇为什么String对象的null加上了""就等于"null"字符串了,先给点资料看看: 这个是我找的一个人博客上的截图 ...

  8. java和python细节总结和java中string 的+操作

    //JAVA中对arrayList的初始化,能够分配空间,不能之间让一个ArrayList赋值给另外一个ArrayList,这样是引用赋值,当一个改变时候,另外一个也改变 List<String ...

  9. string的+操作与StringBuilder对象

    习惯在C#代码中写str+="xxx";这样代码的请注意啦,如果这种操作是针对单个变量作很多次叠加操作的,很有可能导致性能降低. 大家都知道string与StringBuilder ...

  10. Redis - string类型操作

    以个人信息为例操作string类型 设置操作: set:     set key value            创建key-value名值对 setnx:      setnx key value ...

随机推荐

  1. MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)

    本节开始为大家讲解偏向应用的知识--创建对话框.  对话框,大家应该很熟悉了,在我们常用的软件中大多都有对话框界面,例如,360安全卫士的主界面其实就是对话框,知识它做了很多美工方面的工作,将其大大美 ...

  2. Spring集成JPA提示Not an managed type

    在做Spring与JPA集成时,出现问题如下: Caused by: java.lang.IllegalArgumentException: Not an managed type: class co ...

  3. 转:随机函数 C++中rand()函数的用法

    转自:http://blog.163.com/wujiaxing009@126/blog/static/719883992011113011359154/ 一.C++中不能使用random()函数   ...

  4. DB2数据库中SQL语句中使用or和and的关键字的时候注意事项

    --正确的SQL语句,查询结果:746 ) FROM EHR_BASE EB, EHR_HF_INDICATOR EHI WHERE EB.EHR_ID=EHI.EHR_ID ' ' ' AND EB ...

  5. VB6 GDI+ 入门教程[4] 文字绘制

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[4] 文字绘制 2009 年 6 月 18 日 7条评论 ...

  6. linux笔记:linux软件包管理,软件安装位置

    linux软件包简介 软件包分类:1.源码包(用C语言等编写的源代码,没有进行编译):脚本安装包(对源码包进行了安装优化的源码包)优点:开源,可修改可以自由选择所需的功能编译安装,更适合自己的系统,稳 ...

  7. iOS 面试基础题目

    转载: iOS 面试基础题目 题目来自博客:面试百度的记录,有些问题我能回答一下,不能回答的或有更好的回答我放个相关链接供参考. 1面 Objective C runtime library:Obje ...

  8. 反编译工具reflector破解方法

    文件下载:http://files.cnblogs.com/lori/Red.Gate.Reflector7.6.rar 破解方法: 1. 断网2. 运行.NET Reflector,点击Help - ...

  9. 让ie678支持css一些属性及html标签

    昨天写的一个页面,用的css3及html5的一些样式与标签,在ie8下看是没有效果的,然后就在晚上查找了一下如何能让ie8也能实现这些效果. 1.添加respond.js文件,Respond.js让I ...

  10. VMware vSphere Client5.0与 Windows8不再有问题,解决VMware 5.0 客户端提示VMRC控制台的连接已断开

    问题:VMware 5.0 客户端提示VMRC控制台的连接已断开...正在尝试重新连接,系统是win8的 网上解决办法: WIN8,在安装vmware vsphere client 5.0时出现兼容性 ...