#include<string> 与 #include<string.h> 这是两个完全不同的头文件,前者用于C++,后者用于C,一般把这两个头文件都包括进去。 越来越觉得需要对 string 中的一些函数的使用进行一次总结了,这几天碰到了好多关于字符串处理的问题,下面介绍一下 string 中常用的函数;

一、string 类对象的初始化

  string s("Hello !") ;

  string s = "Hell0 !" ;

  string s(10,'a') ;    // 字符串  s 为 : aaaaaaaaaa 10个a

  string s , s = 'a' ;    //将字符 a 赋值给字符串 s ,相当于 s = "a"

二、string 中的 strlen 和 length 函数

  string s1 ;

  char s2[100] ;

  strlen(s2) ;  // 返回字符串 s2 的长度 ;

  s1.length() ;  //返回字符串 s1 的长度 ,相当于 strlen(s1.c_str()) ;

三、string 中的 find 、find_first_of、find_last_of、find_first_not_of、find_last_not_of  函数

  string s("Hello World !") ;

  s.find("o") ;   //  返回的的 o 所在的下标 ;

  s.find("o",5) ;  // 从下标为 5 开始进行查找 ;

  s.append(s , s.find("o") , 3) ;  // 在字符串 s 后面追加 一个字符串("o W") ;

  s.find_fist_of("abc") ;   //返回字符串abc中任意一个字符最先出现的位置

  s.find_last_of("abc") ;  // 返回字符串abc中任意一个字符最后出现的位置

  s.find_first_not_of("acd") ;   //从字符串 s 中进行查找(从前往后),第一个不在字符串 acd 中的位置

  s.find_last_not_of("acd") ;  //从字符串 s 中进行查找(从后往前),第一个不在字符串 acd 中的位置

四、string 中的 erase 函数

  string s("Hello World !") ;

  s.erase(5) ;  // 相当于保留当前长度为 5 ,也相当于s[5] = '\0' ;

五、string 中的 replace 函数

  string s("Hello World !") ;

  s.replace(2,3,"haha") ;  // 从下标为 2 开始 ,将连续的三个字符替换成 字符串 haha ;

  s.replace(2,3,"haha",1,2) ;  从下标为 2 开始 ,将连续的三个字符替换成 字符串 haha 中的 下标为 1 开始的 连续两个字符

六、string 中的 insert 、at 函数

  string s("Hello World !") ;

  s.insert(1 , s ) ;     //在下标为 1 的后面再将 s 插入在其后

  s.insert(1 , s , 1 , 3) ;   // 在下标为 1 的后面将 s 的下标为 1 的连续 3 个字符 插入到 后面

  s.at(1) ;  // 返回下标为 1 所对应的元素,相当于s[1] ;

七、string 中的 c_str 函数

  string s("Hello World !") ;

  printf("%s\n",s.c_str()) ;  //返回传统的 char * 类型的字符串,该字符串以 ‘\0’ 结尾 ;

八、字符数组 中的 strlen、strcat、strcmp 函数

  strlen    返回字符数组的长度

  strcat  连接两个字符数组

  strcmp  比较两个字符数组

九、字符数组中的 strchr 、strstr 函数

  char s[] = {"Hello World !"} ;

  strchr(s , 'o') ;   // 返回字符 o 在字符数组 s 中的位置指针

  strstr(s,"ll") ;   //返回字符串 ll 在字符数组 s 中的位置指针

String VS Cstring(字符串)的更多相关文章

  1. char*、string、CString各种字符串之间转换

    参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...

  2. C++ 中int,char,string,CString类型转换

      1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...

  3. 头文件 string.h cstring string 区别

    1.#include <cstring>   //不可以定义string s:可以用到strcpy等函数using   namespace   std; #include <stri ...

  4. C++ 中 int,char*,string,CString之间相互转换-整理

    <多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...

  5. MFC中char*,string和CString之间的转换

    MFC中char*,string和CString之间的转换 一.    将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如:  CString theString( &q ...

  6. Cpp读文件、CString转String、String转CString

    场景 C++读取文件 技术点 读取文件 fstream提供了三个类,用来实现c++对文件的操作.(文件的创建.读.写). ifstream -- 从已有的文件读入 ofstream -- 向文件写内容 ...

  7. string与CString对比

    string是标准C++库中的字符串类,CString是在Windows开发环境下常用的字符串类,CString目前已从MFC中分离出来可以单独使用,只需包含atlstr.h即可. 相比string, ...

  8. string string.h cstring 区别

    c++中 string与string.h 的作用和区别 #include  <string.h>  void  main()  {        string  aaa=  "a ...

  9. C++中string和char字符串的异同与使用方法

    C++中string和char声明字符串的异同和使用 string类 必须在头文件中包含<string> 隐藏了字符串的数组性质,可以像处理普通变量那样处理字符串 string类位于名称空 ...

随机推荐

  1. Android消息机制不完全解析(下)

    接着上一篇文章Android消息机制不完全解析(上),接着看C++部分的实现. 首先,看看在/frameworks/base/core/jni/android_os_MessageQueue.cpp文 ...

  2. python教程,文章list

    http://www.2cto.com/kf/web/Python/ http://www.v2ex.com/go/python http://www.sharejs.com/codes/python ...

  3. DRP总结

    DRP终于结束了,战线有点长了.记得刚开始听说DRP的时候,感觉这个名词很专业,再加上视频一共有300集,顿时感觉这是一个大项目,很正规.很专业的项目.虽然后来知道DRP知识ERP的一个分支,项目规模 ...

  4. HTML5中video的使用一

    <!DOCTYPE html> <html> <head> <title>HTML5 </title> <meta http-equi ...

  5. md笔记——编程术语

    thinkPython 读书笔记 本列表根据thinkPython中记录的大量编程术语 整理而来. 该书的重点不是教会你Python,而是培养读者用计算机科学家一样的思路来思考,设计,开发. 讲解了语 ...

  6. win7环境下安装MongoDB

    1.从http://www.mongodb.org/downloads获取,下载适合windows版本的mongodb,注意32位和64位的区别2.将下载的zip版本,解压到D:/mongodb3.创 ...

  7. JavaSE思维导图(三)

  8. Oracle树反向查询的优化(转载)

    本文系转载,http://technology.amis.nl/2005/08/11/selecting-a-pruned-tree-with-selected-nodes-and-all-their ...

  9. Models——英语学习小技巧之四

    Models  are very important, here model means role model, is kind of like a hero. It's  someone that ...

  10. 虚拟机NAT模式主机ping不通虚拟机解决方案

    本篇没有抓包,只是简单一个实施.需要的童鞋可以拿走这个方法. 虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST 模式. 桥接就是在真机的网络上模拟一个网卡,给虚拟机申请一个和真机在同一个 ...