如果所比较的两个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. 【转】对于JNI方法名,数据类型和方法签名的一些认识

    [转]对于JNI方法名,数据类型和方法签名的一些认识   之前一直用jni,但是没有考虑Java重载函数,如何在jni-C++里命名,今天看到一篇文章,讲到了类型签名. 原文链接:http://www ...

  2. 在win7 64bit系统安装QC软件

    当本机系统不再QC软件支持的系统上,可以在setup右键选择兼容性选择能支持安装的系统, 在JBOSS页面,选择该服务器,用户名输入本机的用户名和密码,如果没有配置域输入计算机名. 如果没有装IIS, ...

  3. margin:0 auto

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 第20章 数据库操作----JDBC概述

    JDBC是一种可用于执行SQL语句的Java API(Application Programming Interface,应用程序设计接口),是连接数据库和Java应用程序的纽带. 1.JDBC-OD ...

  5. Servlet程序开发--Servlet 与 表单

    servlet程序: doPost方法时为了防止表单提交时post方式的问题.否则只能处理get请求 package org.lxh.servletdemo ; import java.io.* ; ...

  6. 10、桥接模式(Bridge)

    桥接模式就是把事物和其具体实现分开,使他们可以各自独立的变化.桥接的用意是:将抽象化与实现化解耦,使得二者可以独立变化,像我们常用的JDBC桥DriverManager一样,JDBC进行连接数据库的时 ...

  7. Android面试经验2

    1,android如何更换主题: 2,如何设计软件: 3,代码中用到那些设计模式: 4,c++和java有那些不同: 一,指针: 二,多重继承: 三,数据类型和类: 四,自动内存管理: 五,操作符重载 ...

  8. 用CSS让文字居于div的底部

    css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部 ...

  9. div定位

    1.float定位带来的问题<html> <head> <title>div浮动引发的问题</title> </head> <styl ...

  10. java中从含反斜杠路径截取文件名的方法

    例如:获取到的文件路径为C:\Documents and Settings\Leeo\My Documents\logo.gif现在想要取得图片的名称logo.gif,我们知道反斜杠“\”是转义字符, ...