#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. (三)uboot源码分析

    一.九鼎官方uboot和三星原版uboot对比(1)以九鼎官方的uboot为蓝本来学习的,以三星官方的这份为对照.(2)不同版本的uboot或者同一版本不同人移植的uboot,可能目录结构和文件内容都 ...

  2. iOS开发之.pch文件初识

    pch全称是“precompiled header”,即预编译头文件,自Xcode6诞生之日起,便在Supporting Files文件下消失多年.说起苹果对pch的爱恨情仇,其分析pch的作用便不言 ...

  3. HBase之表空间

    1.介绍 在HBase中,namespace命名空间指对一组表的逻辑分组,类似RDBMS中的database,方便对表在业务上划分.Apache HBase从0.98.0, 0.95.2两个版本开始支 ...

  4. Openvpn 安装简述

    1.固定本地IP地址 vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.同步时间.安装ntpd yum  install ntpddate netupda ...

  5. centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误

    centos7 修改selinux 开机导致 faild to load SELinux policy  freezing 错误 之前把selinux关闭了,这次想打开selinux,于是修改了 /e ...

  6. dede顶级栏目直接显示内容

    点击网站栏目管理,增加顶级栏目 在弹出的增加窗口中,填上栏目名称,其它的如果没有特殊的要求可直接默认 点击常规选项旁边的高级选项,并且在列表模板中,将其改一个文件名,这个文件名就是后面需要使用到的文件 ...

  7. 【转】 Linux 线程同步的三种方法

    线程的最大特点是资源的共享性,但资源共享中的同步问题是多线程编程的难点.linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量和信号量. 一.互斥锁(mutex) 通过锁机制实现线程间的 ...

  8. this 指向

    this-->当前行为发生的主体就是this,但是this是谁和这个方法在哪执行的或者在哪定义的都没有半毛钱的关系 如何的区分JS中的this? 1)函数执行,看函数名前面是否有".& ...

  9. 20145218 《Java程序设计》第三周学习总结

    20145218 <Java程序设计>第三周学习总结 教材学习内容总结 定义类 编写程序要产生对象就要先定义类.类是对象的设计图,对象是类的实例.类定义时使用class关键词,建立实例时, ...

  10. HTML5自学笔记[ 18 ]canvas绘图基础5

    获取图像数据:getImgData(x,y,w,h),返回的是一个ImageData对象,这个对象有三个属性保存图像信息:width/height/data.data是一个数组,保存了每个像素的信息, ...