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.判断输入的用户名和密 ...
随机推荐
- element form 校验数组每一项
1.校验的数据结构如下: 2.html结构 <el-dialog :title="title" :visible.sync="visable" width ...
- [转帖]Cacls和ICacls
Cacls和ICacls https://www.cnblogs.com/Aley/p/11089538.html Need Study 解释: Cacls:显示或修改文件的访问控制列表(ACL) ...
- (八)JSP 技术知识点总结(来自那些年的笔记)
目录 什么是 jsp Jsp 调用和运行原理(简略版) Jsp语法 Jsp指令简介 Page指令 Include指令 taglib指令 Jsp乱码问题 jsp运行原理(详细版) JSP中的九大隐式对象 ...
- Python【BeautifulSoup解析和提取网页数据】
[解析数据] 使用浏览器上网,浏览器会把服务器返回来的HTML源代码翻译为我们能看懂的样子 在爬虫中,也要使用能读懂html的工具,才能提取到想要的数据 [提取数据]是指把我们需要的数据从众多数据中挑 ...
- Golang不会自动把slice转换成interface{}类型的slice
目录 例子 原因 如何去实现 例子 我们时常会写一些interface,例如: type A interface{ Print() } type B struct { } func (b *B) Pr ...
- (转)从0移植uboot (四) _点亮调试LED
这一节主要讨论1个问题:点灯.点灯是实际开发中,特别是裸板开发中常见的调试手段,相当于主机开发中漫天飞舞的printf/printk.为了追踪程序的现场执行情况,很多时候我们都使用点一个灯的方法来进行 ...
- WPF 自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分)
#region - 用于绑定ViewModel部分 - public ICommand Command { get { return (ICommand)GetValue(CommandPropert ...
- 使用ef core自动生成mysql表和数据编码的问题
mysql默认的编码是不支持中文的,需要改成utf8编码格式. 而我使用的Pomelo.EntityFrameworkCore.MySql组件生成mysql库和表,他是使用默认编码的. 网上大多说修改 ...
- 一篇文章彻底搞懂异步,同步,setTimeout,Promise,async
之前翻看别的大佬的博客看到了关于setTimeout,promise还有async执行顺序的文章.观看了几篇之后还是没有怎么看懂,于是自己开始分析代码,并整理了此文章,我相信通过此文章朋友们能对异步同 ...
- ntp时间同步简介
网络时间协议(Network Time Protocol) 安装 # sudo apt-get install ntp 官网下载:http://www.ntp.org/downloads.html 本 ...