使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量(对象)的方法:

#include <iostream>
#include <string> using namespace std; int main()
{ string s1; // 只定义不初始化,编译器会赋默认值,默认值为"",即空字符串
string s2 = "C Plus Plus"; // 既定义又初始化,与C风格字符串不同,string结尾没有结束标志'\0'
string s3 = s2; // s3定义时使用s2进行初始化,因此s3内容也是"C Plus Plus"
string s4 (,'s'); // s4被初始化由5个's'字符组成的字符串,也就是"sssss" return ;
}

一.length() 返回字符串长度

string s = "http://www.iosfan.cn";
int len = s.length();
cout<<len<<endl;

注意: 与C不同,string末尾没有'\0'字符,所以length返回的是字符串的真实长度,而不是长度+1

二.c_str() 转换为C风格的字符串

string path = "D:\\demo.txt";
FILE *fp = fopen(path.c_str(), "rt");

三.访问字符串中的字符

#include <iostream>
#include <string>
using namespace std; int main(){
string s = "";
for(int i=,len=s.length(); i<len; i++){
cout<<s[i]<<" ";
}
cout<<endl;
s[] = '';
cout<<s<<endl;
return ;
}

四.字符串拼接

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first ";
string s2 = "second ";
char *s3 = "third ";
char s4[] = "fourth ";
char ch = '@'; string s5 = s1 + s2;
string s6 = s1 + s3;
string s7 = s1 + s4;
string s8 = s1 + ch; cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl; return ;
}

运行结果:
first second
first third
first fourth
first @

五.insert() 插入字符串

#include <iostream>
#include <string>
using namespace std; int main(){
string s1, s2, s3;
s1 = s2 = "";
s3 = "aaa";
s1.insert(, s3);
cout<< s1 <<endl;
s2.insert(, "bbb");
cout<< s2 <<endl;
return ;
}

运行结果

12345aaa67890
12345bbb67890

六 erase() 删除string中的一个子字符串

#include <iostream>
#include <string>
using namespace std; int main(){
string s1, s2, s3;
s1 = s2 = s3 = "";
s2.erase();
s3.erase(, );
cout<< s1 <<endl;
cout<< s2 <<endl;
cout<< s3 <<endl;
return ;
}

运行结果:

七 substr() 截取字符中的一个子字符串

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first second third";
string s2;
s2 = s1.substr(, );
cout<< s1 <<endl;
cout<< s2 <<endl;
return ;
}

运行结果:

first second third
second

八 find() 查找某个字符串出现的位置

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.find(s2,);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return ;
}

运行结果
Found at index : 6

九.rfind() 从第二个参数开始往后查找

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.rfind(s2,);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return ;
}

运行结果:

Found at index : 6

十 find_first_of() 查找子字符串首次出现的位置

#include <iostream>
#include <string>
using namespace std; int main(){
string s1 = "first second second third";
string s2 = "asecond";
int index = s1.find_first_of(s2);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return ;
}

运行结果:

Found at index : 3

C++语言基础(16)-string类的更多相关文章

  1. Java入门 - 语言基础 - 14.String类

    原文地址:http://www.work100.net/training/java-string.html 更多教程:光束云 - 免费课程 String类 序号 文内章节 视频 1 概述 2 创建字符 ...

  2. 16 String类

    java中的所有的字符串文字(例如"abc","123")都可以看做是实现了此类的实例对象 eg: String str = new String(); str ...

  3. C++基础之string类

    string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录. string的操作总结 string(const char *s,int n);  //用c字符串s初始化,s应 ...

  4. Java入门 - 语言基础 - 13.Character类

    原文地址:http://www.work100.net/training/java-character.html 更多教程:光束云 - 免费课程 Character类 序号 文内章节 视频 1 概述 ...

  5. Java基础教程——String类

    String类 Java程序中的所有字符串字面值(如 "abc" )都是String的实例 字符串是常量(因为 String 对象是不可变的,所以可以共享) 字符串的本质是字符数组 ...

  6. c#语言基础编程—string

    引言 在c#中经常会有相关的string的操作,string类型为引用类型,集成于Object,所以会有四个方法.详情可见 值类型和引用类型的区别 里面详细介绍了,值类型和引用类型的区别和应用场合,所 ...

  7. Java基础笔记-String类

    String 类(被final修饰) 字符串是一种特殊的对象,一旦字符串被初始化就不可以被改变了.(内容不变) 例如: String  s = “abc”; String  s1 = new Stri ...

  8. Java基础之String类

    String类 字符串是不可变的,对其做的任何改变,会生成一个对象,不会改变有原有对象. ==和equals() String s1 = "good"; String s2 = & ...

  9. Java 基础之 String 类

    String String 被声明为 final,因此不能被继承.(Integer 等包装类也不能被继承) 在 java8 中,String 内部使用 char 数组 来存储数据 public fin ...

随机推荐

  1. Jenkins上配置Robot Framework测试邮件通知模板

    邮件效果 测试成功如下所示: jenkins_robot_success 测试失败如下所示: jenkins_robot_failure 通过这个模板,我们能够很直观地看出测试的执行情况,以及相关的统 ...

  2. 【kmp算法】poj2406 Power Strings

    如果next[n]<n/2,一定无解. 否则,必须要满足n mod (n-next[n]) = 0 才行,此时,由于next数组的性质,0~n-next[n]-1的部分一定是最小循环节. [ab ...

  3. 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量

    跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...

  4. 【动态规划】【记忆化搜索】CODEVS 1010 过河卒 2002年NOIP全国联赛普及组

    f(i,j)=f(i-1,j)+f(i,j-1),显然可以暴力递归求解,但是很多重复的状态,所以可以记忆下来. 注意障碍点和边界的特判. #include<cstdio> #include ...

  5. pandas操作,感觉不错,复制过来的

    整理pandas操作 本文原创,转载请标识出处: http://www.cnblogs.com/xiaoxuebiye/p/7223774.html 导入数据: pd.read_csv(filenam ...

  6. Android 友盟社会化组件-分享实现

    本文章链接地址:http://dev.umeng.com/social/android/share/quick-integration 分享快速集成 1 产品概述 友盟社会化组件,可以让移动应用快速具 ...

  7. 【sql】sql优化

    sql优化: 1.最左前缀原则 如果contact表上的tenement_id列创建了索引,那么查询的时候将索引列放在最左边,查询的速度会快很多,因为扫描的范围是索引范围而不是整张表范围!! SELE ...

  8. 常用vim命令合集

    移动命令: h:左移 l:右移 k:上移 j:下移 ^:移动到本行第一个非空白字符上 0:移动到本行第一个字符上 gg:移动到文件头 G = shift + g:移动到文件尾 %:从一个"{ ...

  9. 一起來玩鳥 Starling Framework(5)Multi-Touch

    這篇來談談Starling的Multi-Touch.前一篇也提到,Multi-Touch一樣是監聽TouchEvent.TOUCH,然後由TouchEvent的e.getTouches()取回多點的資 ...

  10. Oculus rift DK2 新手使用设置

    为了获得更好的3D沉浸感体验,降低使用晕眩的可能性,使用DK2前,一定要针对使用者自身对DK2进行正确的设置.下面解释一下配置面板的一些参数和意义: Eye Relief滑竿应该和你的DK2两侧的调节 ...