• 初始化用法
#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类基本使用的更多相关文章

  1. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  2. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  3. C++ string类的实现

    c++中string类的实现 今天面试被考到了, 全给忘记了!!!   //string类的实现 #include <iostream> #include <string.h> ...

  4. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...

  5. java基础复习:final,static,以及String类

    2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...

  6. String类和StringBuffer类的区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  7. 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...

  8. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  9. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  10. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

随机推荐

  1. 如何查看USB是不是3.0版本

    打开设备管理器 找到>便携设备 对应U盘,打开属性>查看详细信息>如果设备描述为Data Traveler 3.0 那么这就是3.0的U盘

  2. S04_CH01_搭建工程移植LINUX/测试EMMC/VGA

    S04_CH01_搭建工程移植LINUX/测试EMMC/VGA 1.1概述: 本章内容是在已经提供安装了VIVADO2015.4 的ubuntu系统下,进行.大家可以下周我们已经提供的虚拟机镜像,我们 ...

  3. ActiveMQ的静态网络配置

    static networkConnector是用于创建一个静态的配置对于网络中的多个Broker做集群,这种协议用于复合url,一个复合url包括多个url地址. <networkConnec ...

  4. QMetaEnum获取枚举元信息

    QMetaEnum 类提供了一个枚举的元数据.我们可以使用该类的静态模板函数,fromType<enumerator>来获得关于某个枚举的QMetaEnum对象,然后就可以调用该类的成员函 ...

  5. 2019牛客多校四 E. triples II (容斥)

    大意: 给定$n,a$, 求$n$个$3$的倍数, $or$和为$a$的方案数. 简单容斥题 可以求出$f_{x,y}$表示所有$3$的倍数中, 奇数位不超过$x$个$1$, 偶数位不超过$y$个$1 ...

  6. hdu 4857 反向拓扑问题

    尤其要注意拓扑的分层问题 不难理解 就是不怎么好想到 拓扑的思路这里就不累述了 #include <iostream> #include <cstdio> #include & ...

  7. Html5+Mui前端框架,开发记录(二):提交不了数据?

    1.Mui 请求Webapi接口,返回所需要的数据(get,post) mui.ajax({ url: getAddress() + '/api/Qiliu/get?jsoncallback=?', ...

  8. 七、Flex 布局

    布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 2009年,W3C 提出了一种新 ...

  9. java 框架-分布式服务框架1ZooKeeper

    https://www.cnblogs.com/felixzh/p/5869212.html Zookeeper的功能以及工作原理   1.ZooKeeper是什么?ZooKeeper是一个分布式的, ...

  10. vue拦截

    ```javascript import Vue from 'vue' import App from './App.vue' import router from './router' import ...