如果所比较的两个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类的使用方法的更多相关文章

  1. 【转载】C#中string类使用Replace方法来替换字符串

    在C#的字符串操作过程中,有时候需要替换字符串中的某个子字符串,此时就可以使用到字符串类自带的Replace方法来实现,Replace方法将查找到所有符合被替换的子字符串,然后将之全部替换为目标字符串 ...

  2. 【转载】C#中string类使用Remove方法来移除指定位置的字符

    在C#的字符串操作过程中,有时候需要将字符串中指定位置的字符移除,此时就可能使用到字符串类string类中的Remove方法,此方法允许指定移除开始的开始的索引位置,以及移除的长度信息等,共有2个重载 ...

  3. 【转载】C#中string类使用Substring方法截取字符串

    在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...

  4. Java中String类的format方法使用总结

    可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...

  5. 关于Java中String类的hashCode方法

    首先来看一下String中hashCode方法的实现源码 public int hashCode() { int h = hash; if (h == 0 && value.lengt ...

  6. JAVA中String类的intern()方法的作用

    一般我们变成很少使用到 intern这个方法,今天我就来解释一下这个方法是干什么的,做什么用的 首先请大家看一个例子: public static void main(String[] args) t ...

  7. 103、Java中String类之compareTo()方法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. 098、Java中String类之charAt()方法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  9. JAVA中string类的split方法

    split([separator,[limit]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度

随机推荐

  1. android上的JAVA8:使用retrolambda

    android上的JAVA8:使用retrolambda posted by juuda 2015年6月3日 下午7:33 under Android Java8引入了lambda表达式,让许多开发者 ...

  2. eclipse修改豆沙绿

    长时间的使用eclipse开发会很累吧  设置一个保护眼睛的豆沙绿色 不刺眼 是不是会更好一些呢 那么如何设置呢现在就教大家   工具/原料 eclipse jdk 方法/步骤 1 首先打开eclip ...

  3. [转]理解SSL(https)中的对称加密与非对称加密

    加密 解密 Tweet   密码学最早可以追溯到古希腊罗马时代,那时的加密方法很简单:替换字母. 早期的密码学 古希腊人用一种叫 Scytale 的工具加密.更快的工具是 transposition ...

  4. Cloudsim 3.0在myclipse下的安装过程

    (1)下载cloudsim 3.0: http://code.google.com/p/cloudsim/downloads/list (2)下载flanaga.jar包  下载地址:http://w ...

  5. Ubuntu下安装Reids

    安装 官网  http://redis.io/    下载安装包 redis-3.0.5.tar.gz 解压 tar -zxvf redis-3.0.5.tar.gz cd redis-3.0.5 安 ...

  6. Linux学习 -- 用户和用户组管理

    1 用户配置文件 1.1 用户信息文件 /etc/passwd 查看帮助 man 5 passwd -- account:password:UID:GID:GECOS:directory:shell ...

  7. CodeForces 190D Non-Secret Cypher

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  8. iOS 一招搞定去掉字符串开始的0,尤其是针对时间格式化

    // 2.利用整型自动去掉开头的0, 不要循环和判断prefix以及截取字符串- (NSString *)pp_formatDateWithArrYMDToMD;{    NSInteger mont ...

  9. Not a million dollars ——a certain kind of ingenuity, discipline, and proactivity that most people seem to lack

    原文:http://ryanwaggoner.com/2010/12/you-dont-really-want-a-million-dollars/a certain kind of ingenuit ...

  10. Android----paint触摸轨迹监听

    paint触摸轨迹监听,主要是三种而已,ACTION_DOWN,ACTION_MOVE,ACTION_UP public boolean onTouchEvent(MotionEvent event) ...