#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. Android中Activity、Service和线程之间的通信

    Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...

  2. C++——输入、输出和文件

    一.C++输入和输出概述 1.1.流和缓冲区 C++程序把输入和输出看作字节流.输入时,程序从输入流中抽取字节:输出时,程序将字节插入到输出流中.对于面相文本的程序,每个字节代表一个字符,更通俗地说, ...

  3. 【CDN】域名无法访问,ping不到,tracert不到

    背景:香港服务器,CDN服务商:Incapsula 1.首先猜测,域名是否被墙 原因:ip可以直接访问到网站,其他域名指向服务器也可访问 排查:1)首先理解,怎样才算被墙:大陆外可以通过该域名访问,大 ...

  4. Fatal error compiling: 无效的目标发行版: 1.8 -> [Help 1] (zhuan)

    http://blog.csdn.net/z18137017273/article/details/53033613 ***************************************** ...

  5. HBase集群搭建

    HBase集群搭建 搭建环境:假设我们的linux环境已经准备好,包括网络.JDK.防火墙.主机名.免密登录等都没有问题,而且一定要有zookeeper.下面我们用3台linux虚拟机来搭建Hbase ...

  6. weblogic 下载安装部署说明

    http://blog.csdn.net/gaofuqi/article/details/36870887/ http://jingyan.baidu.com/article/d8072ac45f57 ...

  7. 联想VIBE UI 固件ROM刷机包集合

    固件下载_联想乐问吧http://ask.lenovomobile.com/?c-157.html 联想VIBE UI 固件ROM刷机包集合 悬赏分:0     解决时间:2014/09/12 15: ...

  8. 任务栏右键工具栏里的语言栏没有的修复.reg

    任务栏右键工具栏里的语言栏没有的修复.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{540D8A8B-1C3F- ...

  9. 如何用腾讯云打造一款微视频APP

    版权声明:本文由腾讯云原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/196 来源:腾云阁 https://www.qclo ...

  10. 能源项目xml文件 -- app-datasource.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...