#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. XCode破解真机调试

    XCode破解真机调试  3.0 一.这样做以后能怎样 以device模式编译出app 可以再越狱后的设备上运行 二.要会点什么 命令行,也就是terminal.终端.控制台... vim 三.开始吧 ...

  2. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  3. Python的迭代器(iterator)和生成器(constructor)

    一.迭代器(iterator) 1.迭代器的概述 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器 ...

  4. 简单的Cookie登录

    登录页前台代码 <form id="form1" action ="" method="post"> <input typ ...

  5. 转发年浩大神的spfa算法

    http://www.cnblogs.com/superxuezhazha/p/5426624.html #include<iostream> #include<stdio.h> ...

  6. MSSQL数库备份与还原脚本(多个库时很方便)

    每次通过 Management Studio 的界面操作备份或还原数据库,对于单个数据库还好,要是一次要做多个.那就还是用脚本快些,下面有两段脚本分享一下. ===================== ...

  7. office2010安装出错,windows installer服务不能更新一个或多个受保护的windows文件

    转自:http://www.08lr.cn/article/1985.html office2010安装过程中出现如下图错误:windows installer 服务不能更新一个或多个受保护的wind ...

  8. ASP.NET MVC View向Controller传值方式总结

    1:QueryString传值1)也可以使用new{}来为form的action增加querystring2)在controler里使用Request.QueryString["word&q ...

  9. C# 微信公众平台开发(1)-- 服务器配置

    题记:最近公司需要开发微信服务号,由本人负责,以前虽然听过微信开发,但并没有认真的去了解,项目开发中,也边看文档边开发,记录自己的项目开发经验: 1.注册帐号--填写服务器配置 在https://mp ...

  10. Java学习之网络编程

    转自:http://blog.csdn.net/driverking/article/details/6573992 一.网络编程基本概念 1.OSI与TCP/IP体系模型 2.IP和端口 解决了文章 ...