c++ string类基本使用
- 初始化用法
#include <iostream>
#include "string"
using namespace std;
void main()
{
string m1 = "陈培昌";
string m2("付高峰");
string m3 = m2;
cout<<"m1:"<<m1<< endl;
cout<< "m2:" << m2 << endl;
cout<< "m3:" << m3 << endl;
}
- 三种遍历方式
void main()
{
string chroums = "Deep love is a burning fire Stay";
//方法一:数组遍历
int i;
for (i=;i<chroums.length();i++)
{
cout << chroums[i];
}
cout<<endl << "==============================" << endl;
//方法二:迭代器
for (string::iterator it = chroums.begin(); it != chroums.end(); it++)
{
cout << *it;
}
string myequal(, '*');//一次性生成30个*
cout<<endl<<myequal << endl;
//方法三:at()
for (i = ; i < chroums.length(); i++)
{
cout << chroums.at(i);
}
string anotherequal(, '$');
cout << endl << anotherequal << endl;
}
输出结果:

- 选择at()方法遍历的好处----可以捕捉异常,注意示例中,故意越界访问
void main()
{
string chroums = "Deep love is a burning fire Stay";
string myequal(, '*');//一次性生成30个*
cout << endl << myequal << endl;
//方法三:at()
int i = ;
try
{
for (i = ; i < chroums.length() + ; i++)
{
cout << chroums.at(i);
}
}
catch (...)
{
cout << endl;
cout << "crisis happend" << endl;
}
system("pause");
}
输出结果:

- 而选用其他方式遍历,尽管采取措施捕捉异常,仍旧无法制止错误
#include <iostream>
#include "string"
using namespace std;
void main()
{
string chroums = "Deep love is a burning fire Stay"; //方法二:迭代器
try
{
for (string::iterator it = chroums.begin(); it != chroums.end()+; it++)
{
cout << *it;
}
}
catch (...)
{
cout << "crisis happend" << endl;
} string anotherequal(, '$');
cout << endl << anotherequal << endl;
}
输出结果:

- 查找目标字符串位置
void main()
{
string mywords = "Brother Louie, Louie, Louie";
size_t step3 = mywords.find("Louie",);//size_t是C语言int类型的一种引用
cout <<"在字符串索引处"<< step3<<"找到目标字符串" << endl;
}
输出结果:

改进:持续查找(偏移量不等于字符串的末尾)
void main()
{
string mywords = "Brother Louie, Louie, Louie";
size_t step3 = mywords.find("Louie",);//size_t是偏移量,在C语言中是int类型的引用
while (step3!=string::npos)
{
cout << "在字符串索引处" << step3 << "找到目标字符串" << endl;
step3 = step3 + ;
step3 = mywords.find("Louie", step3);
}
}
输出结果:

- 替换
void main()
{
string mywords = "徐晓冬卷了一只烤鸭饼,兀自咀嚼了起来。而陈培昌盛了一勺汤,品着陷入了沉思";
mywords.replace(,,"付高峰");
cout << mywords <<endl;
}
输出结果:

- 特别位置上的替换
void main()
{
string mywords = "付高峰卷了一只烤鸭饼,兀自咀嚼了起来。而陈培昌盛了一勺汤,品着陷入了沉思";
mywords.replace(,,"徐晓冬");
size_t cpc = mywords.find("陈培昌",);
mywords.replace(cpc,,"吴子龙");
cout << mywords <<endl;
}
输出结果:

- 截断
void main()
{
string mywords = "Brother Louie, Louie, Louie";
//size_t t = mywords.find("烤羊腿",0);
string::iterator it = find(mywords.begin(),mywords.end(),'L');
if (it!= mywords.end())
{
mywords.erase(it);
}
cout << mywords << endl;
//截断字符串erase()
}
输出:

c++ string类基本使用的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- Eureka如何剔除已经宕机的节点
同一个服务部署了多个实例,在通过网关调用时会随机调用其中一个.但是,当某个服务挂掉之后,依然在注册中心中,依然会随机被调用到,调用时便会超时报错.(主要是开发测试或者演示时需要立即将失效的从注册中心剔 ...
- PCL学习(三) SAC-IA 估记object pose
SAC-IA是基于RANSAC算法的对齐算法 通过降采样提高法向计算.FPFH特征的计算 最后通过SAC-IA计算得到对齐的旋转和平移 #include <Eigen/Core> #inc ...
- Mongodb: com.mongodb.MongoSocketReadException: Prematurely reached end of stream
saveLocationInfo errorcom.mongodb.MongoSocketReadException: Prematurely reached end of stream at com ...
- Acquire and Release Fences
转载自: http://preshing.com/20130922/acquire-and-release-fences/ Acquire and release fences, in my op ...
- [转帖]详解Linux系统inode原理--硬链接、软链接、innodb大小和划分等
详解Linux系统inode原理--硬链接.软链接.innodb大小和划分等 原创 波波说运维 2019-07-17 00:03:00 https://www.toutiao.com/i6713116 ...
- Selenium绕过登录的实现
1.使用命令行启动Chrome:Mac:/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome -remote-debugging ...
- JAVA支持字符编码读取文件
文件操作,在java中很常用,对于存在特定编码的文件,则需要根据字符编码进行读取,要不容易出现乱码 /** * 读取文件 * @param filePath 文件路径 */ public static ...
- PAT(B)1003 我要通过!(Java)
1003 我要通过! 题目 判断字符串是否符合给定的规则.更多内容点击标题. 参考博客 ValarMorghulis的博客 分析 规律:num_a * num_b = num_c.字符串a中字 ...
- ThreadLocal内存泄漏真因探究(转)
出处: 链接:https://www.jianshu.com/p/a1cd61fa22da ThreadLocal原理回顾 ThreadLocal的原理:每个Thread内部维护着一个ThreadLo ...
- Composer安装yii2-imagine 压缩,剪切,旋转,水印
安装:composer require --prefer-dist yiisoft/yii2-imagine 查看是否安装成功, 安装了两个目录分别是 vendor/imagine vendor/yi ...