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. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  2. Samy XSS Worm之源码讲解

    说到Web安全和XSS跨站脚本技术,几乎所有的书都会提到Samy Worm,这是在2005年感染了mySpace社交网络上百万用户的蠕虫.正如Morris蠕虫是互联网第一个蠕虫, Samy Worm则 ...

  3. JavaScript之isNaN()函数讲解

    定义和用法 isNaN() 函数用于检查其参数是否是非数字值. 语法 isNaN(x) 参数 描述 x 必需.要检测的值. 返回值 如果 x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的 ...

  4. Python中的正则表达式regular expression

    1 match = re.search(pat,str)  If the search is successful, search() returns a match object or None o ...

  5. PHP第一课:开发环境配置

    最近在学php,大概了解了一下php的语法结构,以及一些php及基础的知识.由此想到了要亲手试一试:以为以前是学java的用的  ide是myeclipse,所以对eclipse软件布局有特别的钟爱. ...

  6. Codeforces Round #261 (Div. 2)

    第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...

  7. React Native 简介:用 JavaScript 搭建 iOS 应用 (1)

    [编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...

  8. acdream1116 Gao the string!(hash二分 or 后缀数组)

    问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...

  9. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

  10. 修改DevExpress中英文提示,将英文改为中文

    1 : 修改DX 提示框中的英文字符 /// <summary> /// 重写DX弹出框 英文变为中文 /// </summary> public class CHS : De ...