C++string的操作
#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的操作的更多相关文章
- redis 的使用 (基础, key操作, string类型操作)
使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...
- Redis系列-存储篇string主要操作函数小结
通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...
- string的操作
除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...
- Library string type(2)——关于String的操作
关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...
- 022 StringTokenizer替换掉String的操作
一:说明 1.说明 String的操作特别消耗内存,所以可以考虑优化. 二:程序 1.程序修改 这部分程序属于Mapper端的程序,稍微优化一下. 2.程序 //Mapper public stati ...
- string stack操作要注重细节问题
A string S consisting of N characters is considered to be properly nested if any of the following co ...
- java String 的+操作导致的问题
不说别的先看代码截图: 结果如下: 很好奇为什么String对象的null加上了""就等于"null"字符串了,先给点资料看看: 这个是我找的一个人博客上的截图 ...
- java和python细节总结和java中string 的+操作
//JAVA中对arrayList的初始化,能够分配空间,不能之间让一个ArrayList赋值给另外一个ArrayList,这样是引用赋值,当一个改变时候,另外一个也改变 List<String ...
- string的+操作与StringBuilder对象
习惯在C#代码中写str+="xxx";这样代码的请注意啦,如果这种操作是针对单个变量作很多次叠加操作的,很有可能导致性能降低. 大家都知道string与StringBuilder ...
- Redis - string类型操作
以个人信息为例操作string类型 设置操作: set: set key value 创建key-value名值对 setnx: setnx key value ...
随机推荐
- [转载]WEB缓存技术概述
[原文地址]http://www.hbjjrb.com/Jishu/ASP/201110/319372.html 引言 WWW是互联网上最受欢迎的应用之一,其快速增长造成网络拥塞和服务器超载,导致客户 ...
- hdu_3555 bomb
数位动态规划 数位动态规划是求解一个大区间[L, R]中间满足条件Q的所有数字的个数(或者和,或其他)的一种方法.它通过分析每一位上的数字,一般用 dp[len][digit][...] 来表 ...
- python操作mongodb之六自定义类型存储
from pymongo.mongo_client import MongoClient client=MongoClient('192.168.30.252',27017) client=drop_ ...
- JSP HTML error code
<html> <head> <title>Setting HTTP Status Code</title> </head> <body ...
- 慎用GetOpenFileName
这两天发现了一个小问题,经过一上午的排查终于找到了问题的原因--Windows 7的API函数GetOpenFileName竟然有BUG! 请参考下面的MFC代码: CFileDialog dlg(T ...
- 3D动画
先上一道菜 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 《Java程序设计》第一周学习总结
20145224 <Java程序设计>第一周学习总结 教材学习内容总结 通过第一周的学习让我对Java有了个初步的了解,知道了Java分为Java SE.Java EE.Java ME三大 ...
- 周爱民-javascript
从纯化的语言中了解到语言的本质:并以混杂的语言来创造我们的世界,. 程序=算法+结构,动静之间,不变的是本质 了解语言的本质,而不是试图学会一门语言. 本书从语言特性出发,把动态静态.函数 ...
- 注解配置springMvc及向作用域中赋值
1.在applicationContext.xml中配置包扫描器 <!-- 使用注解配置扫描器 --> <context:component-scan base-package=&q ...
- 通过class和id获取DOM元素的区别
1.通过id获取DOM元素的方法:document.getElementById("id名") 2.通过class获取DOM元素的方法:document.getElementsBy ...