• 初始化用法
#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. 《ucore lab1 exercise2》实验报告

    资源 ucore在线实验指导书 我的ucore实验代码 题目:使用qemu执行并调试lab1中的软件 为了熟悉使用qemu和gdb进行的调试工作,我们进行如下的小练习: 从CPU加电后执行的第一条指令 ...

  2. nginx+flask02---概念

    概念理解 wsgiref模块是python提供的,用于测试和学习的简单的WSGI服务器模块. 这个模块监听8000端口(监听端口可以改变),把Http请求,根据WSGI协议,转换application ...

  3. Word 查找替换高级玩法系列之 -- 给数字批量添加空格和下划线

    Word中的查找和替换是一个很强大的功能,很多人都在使用这项功能.查找和替换,顾名思义就是说,查找到符合条件的内容,然后将那些内容替换成我们所需要的内容.下面,我们就通过实例来了解一下查找和替换功能, ...

  4. [Tensorflow]激励函数tf.nn.relu样例

    代码: import tensorflow as tf import numpy as np ### 定义添加神经网络层函数 START ### def add_layer(inputs,in_siz ...

  5. (三)mybatis之通过接口加载映射配置文件

    1.1  需求 通过(二)在全局配置文件 mybatis-configuration.xml 通过 <mappers> 标签来加载映射文件,那么如果我们项目足够大,有很多映射文件呢,难道我 ...

  6. Mac下面配置oh-my-ssh

    想了想,把微博里的转到这里来比较靠谱 配置oh-my-ssh: 1.git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh ...

  7. C++编译 C # 调用方法

    C++编译    C # 调用方法 编译时使用  public ref class ABC {   ... }; 调用时  右键---引用 --- 添加dll引用  即可

  8. vscode 基本知识以及如何配置 C++ 环境

    参考: 在用VSCode? 看完这篇文章, 开发效率翻倍!最后一条厉害了~ Visual Studio Code(VS code)你们都在用吗?或许你们需要看一下这篇博文 按下 ctrl+K,再按下 ...

  9. 针对IE6 7 8当独写样式

    IE8的格式: .foot{padding:12px 10px\9;} //在后面加\9 IE7的格式: .foot{*padding:12px 10px\9;} //在前面加* IE6的格式: .f ...

  10. CSS最常用的三种选择器

    标签选择器 样式的名称和标签的名称相同,如示例中的p标签,则对应名称为p的样式,若页面中有多个p标签,则这些p标签共同享用该样式 p{ color:blue; } <p>标签选择器< ...