string类的用法总结
string中常见的成员函数


示例代码:
string s= string("abcdefg");
char ch[] = "abcdefgd";
//调用构造函数全部复制
string str1 = string(s);
//构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str2
string str2 = string(s,2,5);
//复制ch前5个字符串
string str3 = string(ch,5);
//将5个'6'赋给字符串
string str4 = string(5,'6');
//
string str5 = string(s.begin(),s.end());
cout << "oringin s = " + s << endl;
cout << "oringin ch = " <<ch << endl;
cout << "str1 = " + str1<< endl;
cout << "str2 = " + str2<<endl;
cout << "str3 = " + str3<<endl;
cout << "str4 = " + str4<<endl;
cout << "str5 = " + str5<<endl;
输出结果:

string的常用方法
取string中元素
使用at()或[]去字符串的元素
string s = "hello";
char c = s.at(2);
char ch = s[2];
上面两行的效果是一样的,都是将l取出。
字符串比较
compare函数
函数原型如下所示:int compare (const basic_string& s) const;
int compare (const Ch* p) const;
int compare (size_type pos, size_type n, const basic_string& s) const;
int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;
函数返回值介绍:若参与比较的两个串值相同,则函数返回 0;若字符串 S 按字典顺序要先于 S2,则返回负值;反之,则返回正值。下面举例说明如何使用 string 类的 compare() 函数。
具体示例如下:
string s1 = "Hello";
string s2 = "Ok";
string s3 = "abdcds";
string s4("abdcds");
cout << "s1 = " + s1 <<endl;
cout << "s2 = " + s2 <<endl;
cout << "s3 = " + s3 <<endl;
cout << "s4 = " + s4 <<endl;
cout<<"s1.comapre(s2) = " << s1.compare(s2) <<endl;
cout<<"s2.comapre(s3) = " << s2.compare(s3) <<endl;
cout<<"s3.comapre(s4) = " << s3.compare(s4) <<endl;
输出结果如下:
s1 = Hello
s2 = Ok
s3 = abdcds
s4 = abdcds
s1.comapre(s2) = -1
s2.comapre(s3) = -1
s3.comapre(s4) = 0
- 比较运算符
String 类的常见运算符包括 >、<、==、>=、<=、!=。其意义分别为"大于"、"小于"、"等于"、"大于等于"、"小于等于"、"不等于"
示例用法:
#include <iostream>
#include <string>
using namespace std;
void TrueOrFalse (int x)
{
cout << (x?"True":"False")<<endl;
}
int main ()
{
string S1 = "DEF";
string CP1 = "ABC";
string CP2 = "DEF";
string CP3 = "DEFG";
string CP4 ="def";
cout << "S1= " << S1 << endl;
cout << "CP1 = " << CP1 <<endl;
cout << "CP2 = " << CP2 <<endl;
cout << "CP3 = " << CP3 <<endl;
cout << "CP4 = " << CP4 <<endl;
cout << "S1 <= CP1 returned ";
TrueOrFalse (S1 <=CP1);
cout << "S1 <= CP2 returned ";
TrueOrFalse (S1 <= CP2);
cout << "S1 <= CP3 returned ";
TrueOrFalse (S1 <= CP3);
cout << "CP1 <= S1 returned ";
TrueOrFalse (CP1 <= S1);
cout << "CP2 <= S1 returned ";
TrueOrFalse (CP2 <= S1);
cout << "CP4 <= S1 returned ";
TrueOrFalse (CP4 <= S1);
cin.get();
return 0;
}
输出结果:
S1= DEF
CP1 = ABC
CP2 = DEF
CP3 = DEFG
CP4 = def
S1 <= CP1 returned False
S1 <= CP2 returned True
S1 <= CP3 returned True
CP1 <= S1 returned True
CP2 <= S1 returned True
CP4 <= S1 returned False
字符串内容的修改
使用append()函数
basic_string& append (const E * s); //在原始字符串后面追加字符串s
basic_string& append (const E * s, size_type n);//在原始字符串后面追加字符串 s 的前 n 个字符
basic_string& append (const basic_string& str, size_type pos,size_type n);//在原始字符串后面追加字符串 s 的子串 s [ pos,…,pos +n -1]
basic_string& append (const basic_string& str);
basic_string& append (size_type n, E c); //追加 n 个重复字符
basic_string& append (const_iterator first, const_iterator last); //使用迭代器追加
示例代码如下:
string s1 = "123456";
string s2 = "abcdefgh";
string str;
str.assign(s1);
cout <<"after use assign: str = " << str << endl;
使用insert函数
basic_string& insert (size_type p0 , const E * s); //插人 1 个字符至字符串 s 前面
basic_string& insert (size_type p0 , const E * s, size_type n); // 将 s 的前 3 个字符插入p0 位置
basic_string& insert (size_type p0, const basic_string& str);
basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //选取 str 的子串
basic_string& insert (size_type p0, size_type n, E c); //在下标 p0 位置插入 n 个字符 c
iterator insert (iterator it, E c); //在 it 位置插入字符 c
void insert (iterator it, const_iterator first, const_iterator last); //在字符串前插入字符
void insert (iterator it, size_type n, E c) ; //在 it 位置重复插入 n 个字符 c
示例代码如下:
string A("ello");
string B ;
B.insert(1,A);
cout << B << endl;
A = "ello";
B = "H";
B.insert (1,"yanchy ",3);
cout<< B <<endl;
A = "ello";
B = "H";
B.insert (1,A,2,2);
cout << B << endl;
A="ello";
B.insert (1 , 5 , 'C');
cout << B << endl;
字符串替换函数
方法原型:
basic_string& replace (size_type p0, size_type n0, const E * s); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换 1 个字符
basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开
始替换,替换字符 n0 个
basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符
basic_string& replace (iterator first0, iterator last0, const E * s);//使用迭代器替换,和 1) 用法类似
basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 类似
basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 类似
basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 类似
basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替换
示例代码如下:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string var ("abcdefghijklmn");
const string dest ("1234");
string dest2 ("567891234");
var.replace (3,3, dest);
cout << "1: " << var << endl;
var = "abcdefghijklmn";
var.replace (3,1, dest.c_str(), 1, 3);
cout << "2: " << var << endl;
var ="abcdefghijklmn";
var.replace (3, 1, 5, 'x');
cout << "3: " << var << endl;
string::iterator itA, itB;
string::iterator itC, itD;
itA = var.begin();
itB = var.end();
var = "abcdefghijklmn";
var.replace (itA, itB, dest);
cout << "4: " << var << endl;
itA = var.begin ();
itB = var.end();
itC = dest2.begin () +1;
itD = dest2.end ();
var = "abodefghijklmn";
var.replace (itA, itB, itC, itD);
cout << "5: " << var << endl;
var = "abcdefghijklmn";
var.replace (3, 1, dest.c_str(), 4); //这种方式会限定字符串替换的最大长度
cout <<"6: " << var << endl;
return 0;
}
输出结果 :
1: abc1234ghijklmn
2: abc234efghijklmn
3: abcxxxxxefghijklmn
4: 1234
5: 67891234efghijklmn
6: abc1234efghijklmn
字符串查找find
string类的用法总结的更多相关文章
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- VC++ 标准C++中的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- [C++][语言语法]标准C++中的string类的用法总结
转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含 #include ...
- 标准C++中string类的用法
转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...
- 标准C++中的string类的用法总结(转)
http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 ...
- 【C++】C++中的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- string类的用法笔记
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- [转]标准C++中的string类的用法总结
原文地址:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常 ...
- String类的用法
练习如何创建一个类. package create; public class Newstring { String aa; public Newstring() { // TODO Auto-gen ...
- 标准C++中string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
随机推荐
- .Net Core 指定编码格式的问题
我们在读取txt文件时,如果文件格式不是utf8,则获取的中文会乱码,所以要么另存文件为utf8格式,要么使用和文件相同的编码来读取. 如果文件为utf8,则: //一种 StreamReader s ...
- C# 特性和索引(C#学习笔记06)
特性 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签. 特性可以当成一个特殊的类看待 列举特性语法: [attribute(pos ...
- iOS - 适配 iOS 13 之工兵连扫雷
iOS 13 支持适配的机型 目前最新 iPhone 11.iPhone 11 Pro和iPhone 11 Pro Max iPhone X.iPhone XR.iPhone XS.iPhone XS ...
- MongoDB常用数据库命令第二集
=======================基础命令======================= mongo 进入数据库操作界面db 查看当前使用的数据库show dbs 查看当前已经被创建出来的 ...
- Java 面向对象—非静态代码块
一.非静态代码块 1.声明格式 [修饰符] class 类名 { { 非静态代码块 } } 2.非静态代码块中的代码执行时机 (1)在"每次"创建对象的时候执行 (2)比构造方法早 ...
- C# 判断域名或ip+端口号 是否能正常连接?
private static ManualResetEvent TimeoutObject = new ManualResetEvent(false); /// <summary> /// ...
- android黑白屏的问题
你会很奇怪,为什么有些app启动时,会出现一会儿的黑屏或者白屏才进入Activity的界面显示,但是有些app却不会如QQ手机端,的确这里要做处理一下.这里先了解一下为什么会出现这样的现象,其实很简单 ...
- Odoo Qweb语法
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826202.html 一:简介 QWeb是一个基于xml的模板引擎,用于生成HTML片段和页面. 模板指令 ...
- 缓冲加载图片的 jQuery 插件 lazyload.js 使用方法详解
在写代码的时候,经常会用到懒加载的模式,以前是通过window.onload的模式去加载,但是图片很多或者用ajax请求的时候,就会很麻烦,现在用lazyload的模式加载方便很多 <!doct ...
- 检测一个js写的URL拼接函数
有时,我看代码不太理解时,直接调用函数进行输出,是很一个不错的习惯. 今天遇到的调试的结果如下. <script> const U = function (opt, url) { var ...