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]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度
随机推荐
- Android 系统编译
最近研究了下Android 的编译系统,下面结合编译我们自己的产品 mobot 来对整个编译系统进行必要的介绍,方便大家今 后对默认编译的修改. 先列出几个觉得重要的Make 文件: build/bu ...
- php csv操作
csv的写入数据: $data = array( array('qq号','登录时间','名称'), array('123456','2012-08-21 15:21:10'.chr(1),'我是来测 ...
- location对象的属性和方法应用(解析URL)
本文将与大家分享下location对象使用属性和方法来解析URL的实例,感兴趣的朋友可以参考下,希望对你有所帮助 location对象提供了很多属性和方法用来解析URL. 复制代码代码如下: &l ...
- Windows Server 2003 下如何安装及配置 FTP 服务器(转)
Windows Server 2003 下如何安装及配置 FTP 服务器 一.安装 FTP 服务器组件: 写在这里的一点 : 安装及配置 FTP 服务器之前 , 必须先手工配置服务器本身的 IP 地址 ...
- WebClient 访问间歇性返回403解决方案
说明:前段时间做的一个项目莫名的返回403的错误,这种情况也多大是程序员最不喜欢的了,没办法先来分析一下错误信息.之前的代码如下: WebClient webclient = new WebClien ...
- Smarty模版
smarty.inc.php <?php //创建一个实际路径 define('ROOT_PATH',dirname(__FILE__)); //引入Smarty require ROOT_PA ...
- CGAffineTransform 放射变换解析 即矩阵变换
转载自;http://www.cocoachina.com/bbs/3g/read.php?tid=151136 矩阵乘法A B相乘要A的列数等于B的行数才有定义,结果是一个 A行B列的矩阵C,C的每 ...
- POJ 2723 HDU 1816 Get Luffy Out
二分答案 + 2-SAT验证 #include<cstdio> #include<cstring> #include<cmath> #include<stac ...
- jQuery动态绑定
一.原始需求 在实际项目的时候,遇到了一个问题,就是通过JS动态生成的元素,无法触发JS事件. 原始的JS代码: $(function () { $(".original").cl ...
- Windows Server 2012如果打开网页慢下载快的话
原来Windows server 2012默认打开了ECN功能(貌似从Windows server 2008之后都默认打开),个人操作系统却没有打开,而办公室网络的确拥塞不小,造成了这种效果.好了, ...