1String概念

 string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别呢。

string和char*的比较

string是一个类, char*是一个指向字符的指针。

string封装了char*,管理这个字符串,是一个char*型的容器。

 string不用考虑内存释放和越界。

string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

 string提供了一系列的字符串操作函数(这个等下会详讲)

查找find,拷贝copy,删除erase,替换replace,插入insert

2string的构造函数

默认构造函数:

string(); //构造一个空的字符串string s1。

拷贝构造函数:

string(const string &str); //构造一个与str一样的string。如string s1(s2)。

带参数的构造函数

string(const char *s); //用字符串s初始化

string(int n,char c); //用n个字符c初始化

3string的存取字符操作

string类的字符操作:

const char &operator[] (int n) const;

const char &at(int n) const;

char &operator[] (int n);

char &at(int n);

operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。

主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。

4从string取得const char*的操作

const char *c_str() const; //返回一个以'\0'结尾的字符串的首地址

5把string拷贝到char*指向的内存空间的操作

 int copy(char *s, int n, int pos=0) const;

把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。

6string的长度

int length() const; //返回当前字符串的长度。长度不包括字符串结尾的'\0'。

bool empty() const; //当前字符串是否为空

7string的赋值

string &operator=(const string &s);//把字符串s赋给当前的字符串

string &assign(const char *s); //把字符串s赋给当前的字符串

string &assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串

string &assign(const string &s); //把字符串s赋给当前字符串

string &assign(int n,char c); //用n个字符c赋给当前字符串

string &assign(const string &s,int start, int n); //把字符串s中从start开始的n个字符赋给当前字符串

8string字符串连接

string &operator+=(const string &s); //把字符串s连接到当前字符串结尾

string &operator+=(const char *s);//把字符串s连接到当前字符串结尾

string &append(const char *s); //把字符串s连接到当前字符串结尾

string &append(const char *s,int n); //把字符串s的前n个字符连接到当前字符串结尾

string &append(const string &s); //同operator+=()

string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾

string &append(int n, char c); //在当前字符串结尾添加n个字符c

9string的比较

int compare(const string &s) const; //与字符串s比较

int compare(const char *s) const; //与字符串s比较

compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

10string的子串

string substr(int pos=0, int n=npos) const; //返回由pos开始的n个字符组成的子字符串



11string的查找 和 替换

查找

int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置

int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置

find函数如果查找不到,就返回-1

int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置

int rfind(const char *s, int pos=npos) const;

int rfind(const string &s, int pos=npos) const;

//rfind是反向查找的意思,如果查找不到, 返回-1



替换

string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s

string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s

void swap(string &s2); //交换当前字符串与s2的值



12String的区间删除和插入

string &insert(int pos, const char *s);

string &insert(int pos, const string &s);

//前两个函数在pos位置插入字符串s

string &insert(int pos, int n, char c); //在pos位置 插入n个字符c



string &erase(int pos=0, int n=npos); //删除pos开始的n个字符,返回修改后的字符串



demo

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

// string的初始化
void initString()
{
	cout << "init of string object\n";
	string s1 = "lucifer";
	string s2("zhang");
	string s3 = s2; // 通过拷贝构造函数来初始化对象s3
	string s4(10, 'a');

	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;
	cout << "s3: " << s3 << endl;
	cout << "s4: " << s4 << endl;
	cout << endl;
}

// 遍历string
void ergodic()
{
	cout << "ergodic of string object\n";
	string s = "lucifer";

	// 1数组方式
	for (unsigned int i = 0; i < s.length(); i++) {
		cout << s[i] << ' ';
	}
	cout << endl;

	// 2迭代器方式
	for (string::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;

	// 3at()方法
	try
	{
		for (unsigned int i = 0; i < s.length() + 3; i++) {
			cout << s.at(i) << ' '; // 抛出异常
		}
		cout << endl;
	}
	catch (...)
	{
		cout << "发生异常\n";
	}
	cout << endl;

	// 如果用下标,程序直接就中止了
	/*
	try
	{
		for (unsigned int i = 0; i < s.length() + 3; i++) {
			cout << s[i] << ' ';
		}
		cout << endl;
	}
	catch (...)
	{

	}
	*/
}

// 字符指针和string的转换
void char2string()
{
	string s = "lucifer";

	// string -> char*
	cout << s.c_str() << endl;

	// string的内容copy,buf中
	char buf[128] = { 0 };
	s.copy(buf, 3, 0); // 注意,只copy3个字符,不会变成C风格的字符串
	cout << "buf: " << buf << endl;
	cout << endl;
}

// string的连接
void stringAppend()
{
	string s1 = "lucifer ";
	string s2 = "zhang";
	s1 = s1 + s2;
	cout << "s1: " << s1 << endl;

	string s3 = "333";
	string s4 = "444";
	s3.append(s4);
	cout << "s3: " << s3 << endl;
	cout << endl;
}

// string查找和替换
void stringFindAndReplace()
{
	string s = "lucifer hello lucifer 111 lucifer 222 lucifer 333";

	int index = s.find("lucifer", 0); // 从位置下标0开始查找
	cout << "index: " << index << endl;

	// 求出lucifer出现的次数,每一次出现的数组下标
	int offIndex = s.find("lucifer", 0);
	while (offIndex != string::npos) {
		cout << "off index: " << offIndex << endl;
		offIndex = s.find("lucifer", ++offIndex);
	}

	// 把s中的lucifer替换成大写
	offIndex = s.find("lucifer", 0);
	while (offIndex != string::npos) {
		cout << "off index: " << offIndex << endl;
		s.replace(offIndex, 7, "LUCIFER");
		offIndex = s.find("lucifer", ++offIndex);
	}
	cout << "after replace: " << s << endl;
	cout << endl;
	// after replace: LUCIFER hello LUCIFER 111 LUCIFER 222 LUCIFER 333
}

// 截断(区间删除)和插入
void stringDelete()
{
	string s = "hello lucifer hello";
	string::iterator it = find(s.begin(), s.end(), 'l');
	if (it != s.end()) {
		s.erase(it);
	}
	cout << "after delete 'l': " << s << endl;
	// after delete 'l': helo lucifer hello

	s.erase(s.begin(), s.end()); // 全部删除
	cout << "after delete all: " << s << endl;
	// after delete all:

	string s2 = "zhang";
	s2.insert(0, "lucifer"); // 头插法
	cout << "after insert: " << s2 << endl;
	// after insert: luciferzhang

	s2.insert(s2.length(), "hello"); // 尾插法
	cout << "after insert: " << s2 << endl;
	// after insert: luciferzhanghello
	cout << endl;
}

void stringTransform()
{
	string s = "LuciferZhang";
	transform(s.begin(), s.end(), s.begin(), toupper);
	cout << "s: " << s << endl;
	// s: LUCIFERZHANG

	transform(s.begin(), s.end(), s.begin(), tolower);
	cout << "s: " << s << endl;
	// s: luciferzhang

	cout << endl;
}

int main()
{
	initString();
	ergodic();
	char2string();
	stringAppend();
	stringFindAndReplace();
	stringDelete();
	stringTransform();

	return 0;
}

最新文章

  1. 网络切片在5G中的应用
  2. java-注解
  3. Stunnel使用
  4. mysql对比表结构对比同步,sqlyog架构同步工具
  5. Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time (转)
  6. tcprstat源码分析之tcp数据包分析
  7. 关于struts2的modelDriven
  8. TCL_事务控制语言
  9. CH Round #53 -密室
  10. poj 3273 Monthly Expense(二分搜索之最大化最小值)
  11. geyear和getfullyear的区别
  12. TCP/IP详解 卷1 第二十章 TCP的成块数据流
  13. PHP实现WebService的简单示例和实现步骤
  14. Java 使用 jacob 将 word 文档转换为 pdf 文件
  15. mysql 触发器 trigger用法 two (稍微复杂的)
  16. WinForm如何去掉右边和下边的白边
  17. html5拖拽初窥
  18. HBase(3)-安装与Shell操作
  19. MDL数据结构
  20. form表单提交数据的数据格式

热门文章

  1. 王家林人工智能AI课程大纲和电子书 - 老师微信13928463918
  2. O(1)空间内实现矩阵转置
  3. (译)快速指南:用UIViewPropertyAnimator做动画
  4. ROS机器人程序设计(原书第2版)补充资料 (拾) 第十章 使用MoveIt!
  5. Android开发指南--0 总览
  6. 使用Intent传递对象
  7. [sed]命令笔记
  8. 10 GridView 样式属性
  9. LATEX TEMPLATE (SPRINGER) (*.BST)
  10. Android开发学习之路--RxAndroid之初体验