string中常用的函数

发现在string在处理这符串是很好用,就找了一篇文章放在这里了..

用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重
1、Define
           string s1 = "hello";
           string s2 = "world";
           string s3 = s1 + "," + s2 +"!\n";
2、append
           s1 += ",shanshan\n";
3、Compare
           if(s1 == s2)
              .....
           else if(s1 == "hello")
              .....
4、 string 重载了许多操作符,包括 +, +=, <, =, , [], <<, >>等,正式这些操作符,对字符串操作非常方便

#include <string>#include <iostream>using namespace std;int main(){           string strinfo="Please input your name:";           cout << strinfo ;           cin >> strinfo;        if( strinfo == "winter" )           cout << "you are winter!"<<endl;        else if( strinfo != "wende" )           cout << "you are not wende!"<<endl;        else if( strinfo < "winter")           cout << "your name should be ahead of winter"<<endl;        else            cout << "your name should be after of winter"<<endl;           strinfo += " , Welcome to China!";           cout << strinfo<<endl;           cout <<"Your name is :"<<endl;           string strtmp = "How are you? " + strinfo;        for(int i = 0 ; i < strtmp.size(); i ++)           cout<<strtmp[i];        return 0;} 

5、find函数
由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:

函数名 描述
find 查找
rfind 反向查找
find_first_of 查找包含子串中的任何字符,返回第一个位置
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置

以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数:

size_type find_first_of(const basic_string& s, size_type pos = 0)size_type find_first_of(const charT* s, size_type pos, size_type n)size_type find_first_of(const charT* s, size_type pos = 0)size_type find_first_of(charT c, size_type pos = 0)

所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。其实string::npos表示的是-1。即没找到就返回-1。例子如下:#include <string>#include <iostream>using namespace std;int main(){           string strinfo="      //*---Hello Word!......------";           string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";        int first = strinfo.find_first_of(strset);        if(first == string::npos) {                    cout<<"not find any characters"<<endl;                return -1;           }         int last = strinfo.find_last_of(strset);        if(last == string::npos) {                    cout<<"not find any characters"<<endl;                return -1;           }            cout << strinfo.substr(first, last - first + 1)<<endl;//string.substr是子串        return 0;}6、insert函数, replace函数和erase函数

string只是提供了按照位置和区间的replace函数,而不能用一个string字串来替换指定string中的另一个字串。例子:#include <string>#include <iostream>using namespace std;int main() {           string strinfo="This is Winter, Winter is a programmer. Do you know Winter?";           cout<<"Orign string is :\n"<<strinfo<<endl;           string_replace(strinfo, "Winter", "wende");           cout<<"After replace Winter with wende, the string is :\n"<<strinfo<<endl;        return 0;}

string.erase(pos,srclen);//srclen是删除的长度string.insert(pos,strdst); //pos是定位,strdst是插入的函数void string_replace(string & strBig, const string & strsrc, const string &strdst) {           string::size_type pos=0;           string::size_type srclen=strsrc.size();           string::size_type dstlen=strdst.size();        while( (pos=strBig.find(strsrc, pos)) != string::npos){                   strBig.erase(pos, srclen);                   strBig.insert(pos, strdst);                   pos += dstlen;           }}

string中常用的函数的更多相关文章

  1. jQuery中常用的函数方法

    jQuery中常用的函数方法总结 Ajax处理 load(url,[data],[callback]) url (String) : 待装入 HTML 网页网址. data (Map) : (可选) ...

  2. SQL点滴30—SQL中常用的函数

    原文:SQL点滴30-SQL中常用的函数 该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很 ...

  3. Mysql中常用的函数汇总

    Mysql中常用的函数汇总: 一.数学函数abs(x) 返回x的绝对值bin(x) 返回x的二进制(oct返回八进制,hex返回十六进制)ceiling(x) 返回大于x的最小整数值exp(x) 返回 ...

  4. string 中的 length函数 和size函数 返回值问题

    string 中的 length函数 和 size函数 的返回值  (  还有 char [ ] 中 测量字符串的  strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 ...

  5. jQuery中常用的函数方法总结

    jQuery中为我们提供了很多有用的方法和属性,自己总结的一些常用的函数,方法.个人认为在开发中会比较常用的,仅供大家学习和参考. 事件处理 ready(fn) 代码: $(document).rea ...

  6. Sass中常用的函数

    字符串函数 To-upper-case() 函数将字符串小写字母转换成大写字母 To-lower-case() 函数 与 To-upper-case() 刚好相反,将字符串转换成小写字母 数字函数 S ...

  7. C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...

  8. MYSQL初级学习笔记八:MySQL中常用的函数!(视频序号:初级_45-50)

    知识点十:MySQL中的函数(45-50) 数学函数: 名称 描述 CEIL() 进一取整 FLOOR() 舍一取整 MOD 取余数(取摸) POWER() 幂运算 ROUND() 四舍五入 TRUN ...

  9. PHP中常用的函数

    1.php 字符串截取函数 2.php取得当前时间函数 3.php 字符串长度函数 4.几种php 删除数组元素方法 5.php中var_dump()函数的详解说明 6.PHP preg_match正 ...

随机推荐

  1. python 二分法查找

    这个也是之前写的程序,现在把它贴上来 #!/usr/bin/python import os os.system('clear') def binsearch(seq,x,low,high): mid ...

  2. MYSQL注入天书之服务器(两层)架构

    Background-6 服务器(两层)架构 首先介绍一下29,30,31这三关的基本情况: 服务器端有两个部分:第一部分为tomcat为引擎的jsp型服务器,第二部分为apache为引擎的php服务 ...

  3. socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto

    socket异步通信-如何设置成非阻塞模式.非阻塞模式下判断connect成功(失败).判断recv/recvfrom成功(失败).判断send/sendto 博客分类: Linux Socket s ...

  4. 通过登入IP记录Linux所有用户登录所操作的日志

    通过登入IP记录Linux所有用户登录所操作的日志 对于Linux用户操作记录一般通过命令history来查看历史记录,但是如果在由于误操作而删除了重要的数据的情况下,history命令就不会有什么作 ...

  5. ASP.NET MVC的处理管线

    原文:http://www.cnblogs.com/fzrain/p/3651693.html 下面开始解释各个部分: 路由模块 1.在ASP.NET MVC处理管线中的第一站就是路由模块.当请求到达 ...

  6. POJ 1656

    #include<iostream>//chengdacaizi 08 .11. 12 #include<string> using namespace std; ][]={} ...

  7. Install WindowBuilder for Eclipse

    WindowBuilder官方下载安装说明地址:http://www.eclipse.org/windowbuilder/download.php 先祝各位能顺利安装上!以下是基于Eclipse in ...

  8. HTTP协议header标头详解

    本文根据RFC2616(HTTP/1.1规范),参考 http://www.w3.org/Protocols/rfc2068/rfc2068 http://www.w3.org/Protocols/r ...

  9. Combination Sum III

    https://leetcode.com/problems/combination-sum-iii/ Find all possible combinations of k numbers that ...

  10. 【XJOI-NOIP16提高模拟训练9】题解。

    http://www.hzxjhs.com:83/contest/55 说实话这次比赛真的很水..然而我只拿了140分,面壁反思. 第一题: 发现数位和sum最大就是9*18,k最大1000,那么su ...