網站首頁 編程語言 正文
string基本概念
本質:
string是c++風格的字符串,而string本質上是一個類
string和char*區別
char*是一個指針string是一個類,類內部封裝了char*,管理這個字符串,是一個char*型的容器
特點:
string類內部封裝了很多成員方法
例如: 查找find,拷貝copy,刪除delete、替換replace、插入 insert
string管理char*所分配的內存,不用擔心復制越界和取值越界等,由內部類進行負責
string初始化
#include<string>
/*
-string(); //創建一個空的字符 例如: string str
string(const char *s); //使用字符串s初始化
- string(const string &str); //使用一個string對象初始化另一個string對象
- string(int n, char c); //使用n個字符c初始化
*/
void test01()
{
string s1; //默認構造
const char* str = "hello world";
string s2(str);
cout << s2 << endl;
//方法3,拷貝構造
string s3(s2);
cout << "s3 = " << s3 << endl;
//方法4
string s4(10,'a');
}
string賦值操作
/*
1、string operator=(const char* s); //char*類型字符串,賦值給當前的字符串
2、string operator=(const string &s); //把字符串s賦值給當前的字符串
3、string operator=(const char); //字符賦值給當前的字符串
4、string& assign(const char *s); //把字符串s賦值給當前的字符串
5、string& assign(const char* s, int n); //把字符串的前n個字符賦值給當前字符串
6、string& assign(const string &s); //把字符串s賦值給當前字符串
7、string& assign(int n, char c); //用n個字符串c賦值給當前字符串
*/
void test01()
{
//方法1
string str1;
str1 = "hello world";
cout << "str1 =" << str1 << endl;
//方法2
string str2;
str2 = str1;
cout << "str2 =" << str2 << endl;
//方法3
string str3;
str3 = 'a';
cout << "str3 =" << str3 << endl;
//方法4
string str4;
str4.assign("hello c++");
cout << "str4 =" << str4 << endl;
//方法5
string str5;
str5.assign(str4, 5);
cout << "str5 =" << str5 << endl;
//方法6
string str6;
str6.assign(str5);
cout << "str6 =" << str6 << endl;
//方法7
string str7;
str7.assign(10,'w');
cout << "str7 =" << str7 << endl;
}
string字符串拼接
/*
1、string& operator+=(const char* str); //重載+=操作符
2、string& operator+=(const char c); //重載+=操作符
3、string& operator+=(const string& str); //重載+=操作符
4、string& append(const char* s); //把字符串s連接到當前字符串結尾
5、string& append(const char *s, int n); //把字符串s的前n個字符連接到當前字符串結尾
6、string& append(const string &s); //同operator+=(const string &str)
7、string& append(const string& s, int pos, int n); //字符串s中從pos開始的n個字符串連接到字符串結尾
*/
void test02()
{
//方法1
string str2_1 = "my ";
str2_1 += "love play game";
cout << "str2_1 =" << str2_1 << endl;
//方法2
str2_1 += 't';
cout << "str2_1 =" << str2_1 << endl;
//方法3
string str2_2;
str2_2.assign("I LOVE LEARN and ");
str2_2 += str2_1;
cout << "str2_2 =" << str2_2 << endl;
//方法4
string str2_3;
str2_3 = "I";
str2_3.append(" Love");
cout << "str2_3 =" << str2_3 << endl;
//方法5
string str2_4 = "aaa";
str2_4.append("bcd", 2);
cout << "str2_4 =" << str2_4 << endl;
//方法6
string str2_5 = "bbb";
str2_5.append(str2_4,0,2);
cout << "str2_1 =" << str2_5 << endl;
}
原文鏈接:https://blog.csdn.net/zzsxyl/article/details/125504909
相關推薦
- 2022-10-14 Mybatis一對多使用PageHelper分頁遇到的問題
- 2021-12-03 Go并發編程中sync/errGroup的使用_Golang
- 2022-07-21 Linux上源碼包安裝nginx及yum 安裝nginx
- 2022-06-27 Golang編程并發工具庫MapReduce使用實踐_Golang
- 2022-10-19 Android?webview加載H5方法詳細介紹_Android
- 2022-04-14 C#可變參數params示例詳解_C#教程
- 2022-10-16 Python?結構化字符串中提取數據詳情_python
- 2022-02-25 C語言函數棧幀的創建和銷毀介紹_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支