C++中string类的使用方法
如果所比较的两个string 相等,则返回0; 操作string 大于参数string,返回
正数;操作string 小于参数string,返回负数。
(1) 比较操作string 与 _Str 或C-string _Ptr
int compare( const basic _ string& _Str ) const;
int compare( const value _ type* _Ptr ) const;
int com = s.compare ( sp );
(2) 比较操作string 中 _Pos1 ( 下标)开始的 _Num1 个字符 与 string _Str
比较操作string 中 _Pos1 ( 下标)开始的 _Num1 个字符 与 C-string _Ptr
比较操作string 中 Pos1 ( 下标)开始的 Num1 个字符 与 Str 中 Off ( 下标)开始 Count 个字
符
int compare( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str );
int compare( size _ type _Pos1 , size _ type _Num1 , const value _ type* _Ptr ) const;
int compare( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str ,
size _ type _Off , size _ type _Count );
int com1 = s.compare ( 2 , 3 , sp );
int com2 = s.compare ( 2 , 3 , c );
int com3 = s.compare ( 1 , 3 , cs , 3 ,1 );
basic_string::erase
删除string 中的一个或几个元素。前两个成员函数,返回要被删除的子串的下
一个元素的iterator; 第三个函数,返回删除后的string 的引用。
(1) 删除string 中从 _ First 到 _ Last 的字符
iterator erase( iterator _First , iterator _Last );
basic_string ::iterator s_Iter;
s_Iter = s.erase ( s.begin ( ) + 3 , s.end ( ) - 1 ); // s_Iter=s.end( )
(2) 删除string 中 _It 所指的字符
iterator erase( iterator _It );
s_Iter = s.erase ( s.begin ( ) + 5 );
(3) 删除string 中从 _Pos ( 下标)开始的 _Count 个字符
basic _ string& erase( size _ type _Pos = 0, size _ type _Count = npos );
str = s.erase ( 6 , 8 ); // str 也是 string
basic_string::find
寻找给定的string。返回找到的第一个string 下标值;如果没找到则返回npos。
(1) 找一个character _Ch 。(默认从头找)
size _ type find( value _ type _Ch , size _ type _Off = 0 ) const;
string s ( "Hello Everyone" );
basic_string ::size_type index1, index2;
static const basic_string ::size_type npos = -1;
index1 = s.find ( "e" , 3 ); // index1=8, 不是 6
index2 = s.find ( "x" ); // index2=-1
if ( indexCh1a != npos ) cout <else cout << "The character 'e' was not found in str1 ." << endl;
(2) 找一个C-string。(默认从头找)
size _ type find( const value _ type* _Ptr , size _ type _Off = 0 ) const;
string s ( "Let me make this perfectly clear." );
basic_string ::size_type index;
const char *c = "perfect";
index = s.find ( c , 5 ); // index=17
(3) 找一个string。(默认从头找)
size _ type find( const basic _ string& _Str , size _ type _Off = 0 ) const;
C++中string类的使用方法的更多相关文章
- 【转载】C#中string类使用Replace方法来替换字符串
在C#的字符串操作过程中,有时候需要替换字符串中的某个子字符串,此时就可以使用到字符串类自带的Replace方法来实现,Replace方法将查找到所有符合被替换的子字符串,然后将之全部替换为目标字符串 ...
- 【转载】C#中string类使用Remove方法来移除指定位置的字符
在C#的字符串操作过程中,有时候需要将字符串中指定位置的字符移除,此时就可能使用到字符串类string类中的Remove方法,此方法允许指定移除开始的开始的索引位置,以及移除的长度信息等,共有2个重载 ...
- 【转载】C#中string类使用Substring方法截取字符串
在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...
- Java中String类的format方法使用总结
可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...
- 关于Java中String类的hashCode方法
首先来看一下String中hashCode方法的实现源码 public int hashCode() { int h = hash; if (h == 0 && value.lengt ...
- JAVA中String类的intern()方法的作用
一般我们变成很少使用到 intern这个方法,今天我就来解释一下这个方法是干什么的,做什么用的 首先请大家看一个例子: public static void main(String[] args) t ...
- 103、Java中String类之compareTo()方法
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 098、Java中String类之charAt()方法
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- JAVA中string类的split方法
split([separator,[limit]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度
随机推荐
- 转载 Deep learning:七(基础知识_2)
前面的文章已经介绍过了2种经典的机器学习算法:线性回归和logistic回归,并且在后面的练习中也能够感觉到这2种方法在一些问题的求解中能够取得很好的效果.现在开始来看看另一种机器学习算法--神经网络 ...
- 初探JavaScript魅力(二)
行为,样式,结构三者分离(javascript, css, html),不要在行间加行为,样式 样式优先级:*<标签<class<ID<行间 style与className,如 ...
- HDU1874 最短路 SPFA
最短路 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 24 Solved: 17 [Submit][Status][Web Board] Descr ...
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- CodeForces 678C Joty and Chocolate
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- java工程开发之图形化界面之(第一课)
下面我们先上代码: package 一个事例图形小应用程序; import javax.swing.JApplet; import java.awt.Graphics; public class 绘制 ...
- EM阅读资料
1,从最大似然到EM算法浅解 2,(EM算法)The EM Algorithm 3,数据挖掘十大算法----EM算法(最大期望算法) (番外)最大后验估计(MAP)
- sphinx query multiple indexes in php
http://stackoverflow.com/questions/17494784/searching-a-particular-index-using-sphinx-from-multiple- ...
- 关于NIOS ii烧写的几种方式(转)
源:http://www.cnblogs.com/bingoo/p/3450850.html 1. 方法一:.sof和.elf全部保存在FPGA内,程序加载和运行也是在FPGA内部. 把FPGA的配置 ...
- Android-----View绘制流程以及invalidate()等相关方法分析 .
引自:http://blog.csdn.net/qinjuning/article/details/7110211 前言: 本文是我读<Android内核剖析>第13章----View工作 ...