如果所比较的两个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. ntp源码解读(一)

    /* * session_key - generate session key * * This routine generates a session key from the source add ...

  2. php实现json

    <?PHP function __json_encode( $data ) { if( is_array($data) || is_object($data) ) { $islist = is_ ...

  3. PHP AJAX技术

    AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX 通过在后台与服务器进行少量数据交换,使网页实现异步更新.这意味着可以在不重载整个页面的情况下,对网页的某些部分进行更 ...

  4. seo步骤

    1. 关键词.找trends主词.去渣渣.(扩展).(去重tool sort) .打乱 :https://adwords.google.com/https://www.google.com/trend ...

  5. JAVA的RSS处理

    一:什么是RSS RSS(really simple syndication) :网页内容聚合器.RSS的格式是XML.必须符合XML 1.0规范. RSS的作用:订阅BLOG,订阅新闻二:RSS的历 ...

  6. zookeeper系列之异步通知模式-Watcher

    1 watcher种类和事件种类 Watcher种类 1. zookeeper实例化时注入的默认Watcher 2. dataWatchers 一个Map<string Set<Watch ...

  7. JAVA基础--继承和权限控制

    1. extends继承 2. java只支持单继承,不允许多继承 修饰符 类内部 同一个包 子类 任何地方 private YES       default YES YES     protect ...

  8. C++ Builder多线程编程技术经验谈(转)

    源:C++ Builder多线程编程技术经验谈 线程之可行性   在很多情况下,可能需要为程序创建线程.这里给出其中一些可能性: (1)如果创建的是一个多文档接口(Multiple Document ...

  9. 安装SqlServer2008后vs中dev控件消失

    点击红的的

  10. PAT (Advanced Level) 1083. List Grades (25)

    简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...