String VS Cstring(字符串)
#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(字符串)的更多相关文章
- char*、string、CString各种字符串之间转换
参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...
- C++ 中int,char,string,CString类型转换
1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...
- 头文件 string.h cstring string 区别
1.#include <cstring> //不可以定义string s:可以用到strcpy等函数using namespace std; #include <stri ...
- C++ 中 int,char*,string,CString之间相互转换-整理
<多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...
- MFC中char*,string和CString之间的转换
MFC中char*,string和CString之间的转换 一. 将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如: CString theString( &q ...
- Cpp读文件、CString转String、String转CString
场景 C++读取文件 技术点 读取文件 fstream提供了三个类,用来实现c++对文件的操作.(文件的创建.读.写). ifstream -- 从已有的文件读入 ofstream -- 向文件写内容 ...
- string与CString对比
string是标准C++库中的字符串类,CString是在Windows开发环境下常用的字符串类,CString目前已从MFC中分离出来可以单独使用,只需包含atlstr.h即可. 相比string, ...
- string string.h cstring 区别
c++中 string与string.h 的作用和区别 #include <string.h> void main() { string aaa= "a ...
- C++中string和char字符串的异同与使用方法
C++中string和char声明字符串的异同和使用 string类 必须在头文件中包含<string> 隐藏了字符串的数组性质,可以像处理普通变量那样处理字符串 string类位于名称空 ...
随机推荐
- mongoose的populate的使用方法;
LotteryReceiveRecord.find({"lottery":req.params.id}).populate("user lottery").ex ...
- XMPP安装中遇到需要卸载openfire的步骤
首先,确保你已经关掉了openfire打开终端 (在应用程序—>实用工具—>)输入以下命令sudo rm -rf /Library/PreferencePanes/Openfire.pre ...
- Matrix Swapping II(求矩阵最大面积,dp)
Matrix Swapping II Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 开源ceph管理平台inkscope部署手册
一.前情提要 关于inkscope就不做过多介绍了,就是ceph的一个开源管理控制平台,跟ceph官方的calamary以及intel的VSM差不多一类,只是各自侧重点不一样. 相对而言,因为inks ...
- 脚本化css
html文档的视觉显示包含很多变量:字体.颜色.间距等.css标准列举了这些变量.我们称之为样式属性.css定义了这些属性以指定字体.颜色.外边距.边框.背景.图片.文本对齐方式.元素尺寸和元素位置. ...
- win7(32 bit) 环境下点击打印预览报错解决办法
如题,报错截图如下 : 解决办法如下: 1.关闭系统数据执行保护.具体操作: 在win7 系统命令行中执行如下命令: bcdedit.exe /set {current} nx Alw ...
- 浅谈SpringMVC(二)
一.SpringMVC的拦截器 1.写类implements HandlerInterceptor public class MyMvcInterceptor implements HandlerIn ...
- 大数据之scala基本语法学习
package edu.snnu.test object list2 { //把字符串转化成一个char类型的list "99 Red Balloons".toList //> ...
- Hive和Jdbc示例
重要:在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口.使用下面命令进行开启:hive -service hiveserver & 1). 测试数据 user ...
- js处理json的方法
var json = "{id:"myid", url:"http://www.myurl.com"}"; var js= (new Fun ...