<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

C++字符串string

嵌入式技術 ? 來源:嵌入式技術 ? 作者:嵌入式技術 ? 2023-07-10 00:26 ? 次閱讀

1.string類簡介

string是C++編程語言中的字符串。在C++中字符串處理可以使用c語言字符串形式char *,也可以使用string類格式。

string 是一個類,類內有char *指針,通過容器方式管理字符串。使用string類型需要需要包含頭文件string。

2.string類的構造函數

string類提供了多種構造函數,支持無參構造、有參構造、拷貝構造。

無參構造:
string()  
有參構造:string(const char *str);
拷貝構造
:string(const string &str);
初始化字符串為count個c字符:string(int count,char c);

使用示例:

#include 
#include 
using namespace std;
void test()
{
	string s1;//默認構造,即無參構造函數
	char const* str = "hello,world";//c語言字符穿
	string s2(str);//傳入C語言字符串
	cout 
wKgaomSqwICAA6-pAAK9VNfkJnU317.png

3.string類賦值相關函數

string類中提供"="運算符重載和assign全局函數進行賦值操。這兩種方式均有多個重載版本,如下所示:

string字符串賦值
string &operator=(const char *s);//將c字符串賦給string
string &operator=(const string &s);//將s賦值給string
string &operator=(char c);//將字符c賦給string
//全局函數
string &assign(const char *s);//將c字符串賦給string
string &assign(const char *s,int n);//將c字符串前n個賦給string
string &assign(const string &s);//將s賦給string
string &assign(int n,char c);//將n個c賦給string

使用示例:

#include 
using namespace std;
#include 
void test()
{
	string s1="hello,world";//c語言字符串賦值
	cout 
wKgaomSqwWmAFeV0AAIlrwTEwq0242.png

4.string類字符串拼接

string類中字符串拼接有重載"+="運算和全局函數append方式,均有多種重載版本。

運算符重載
string &operator+=(const char *s);////將s追加到string
string &operator+=(const string &str);//將str追加到string
string &operator+=(const char c);//將字符c追加到string
全局函數append
string &append(const char *s);//將s追加到string
string &append(const char *s,int n);//將s的前n個追加到string
string &append(const string &str);//等價于operator+=(const string &str)
string &append(const string &str,int pos,int n); --從str的第pos位置開始,取出n個字符追加到string中

使用示例:

void test()
{
	//+=運算符重載示例
	string s1 = "C++";
	s1 += "學習";
	s1 += ',';
	cout 
wKgZomSqwjyAAdpMAAKZmJlCIUk800.png

5.字符串查找與替換

string字符串查找有find、rfind函數。其中find函數是從左往右查找,查找成功返回第一個出現的下標,失敗返回-1;rfind是查找最后一個出現的下標,失敗返回-1。 replace函數實現字符串替換。

//find函數:從左往右查找
返回值:查找成功返回出現的位置,失敗返回-1
int find(const string&str,int pos=0)const;//形參pos表示開始查找的起始位置
int find(const char *str,int pos=0)const;
int find(const char *str,int pos=0,int n)const;//從str的第pos開始的前n個字符中str出現的位置
int find(const char c,int pos=0);//查找字符c
//rfind函數:從右往左查找
int rfind(const string &str,int pos=npos);從pos位置開始查找str最后一次出現的位置
int rfind(const char *str,int pos=npos);從pos位置開始查找str最后一次出現的位置
int rfind(const char *str,int pos=pos,int n);從pos開始的前n個字符中str最后一次出現的位置
int rfind(const char c,int pos=0);//查找c最后一次出現的位置
//字符串替換
string &replace(int pos,int n,const string &s);//將字符串的第pos位置開始的n個字符替換成s
string &replace(int pos,int n,const char *s);

使用示例:

#include 
#include 
using namespace std;
void test()
{
	string str = "1asd3as456asd4789asd";
	int pos = str.find("asd");//查找asd第一次出現的位置
	cout 
wKgaomSqw1GAavHcAAMeFfb75Lo647.png

6.字符串比較

string類中字符串比較函compare數由多個重載版本,既可以和C語言風格const char *字符串進行比較,也可以和string類字符串進行比較。相等返回0,不相等返回!0值。

字符串比較:
int compare(const char *str);//相等返回0,否則返回非0值
//比較string的len個字符,從idx位置開始
int string::compare (size_type idx, size_type len, const string& str) const
//從指定位置指定長度開始比較
int string::compare (size_type idx, size_type len, const string&str, size_type str_idx, size_type str_len) const

使用示例:

#include 
using namespace std;
#include 
void test()
{
	string str1 = "hello,world";
	if (str1 == "hello,world")
	{
		cout 
wKgZomSqxGWAQGaUAAJlt4QiE8o833.png

7.string字符串以字符方式讀寫

string類字符串使用字符方式訪問,可以使用重載"[]"版本函數,即可以和C語言中訪問方式一樣,也可以使用函數at來訪問。

string類中對于字符串的長度獲取,可以使用size()函數或者length()函數來實現。

string以字符方式訪問
	char &operator[](int n);
	char &at(int n);
string 中獲取字符串長度
	str.size();
	str.length();

使用示例:

#include 
#include 
using namespace std;
void test()
{
	string str = "hello,world";
	cout 
wKgZomSqxXWAWiX3AAHNa5Z8Ff8254.png

8.string字符串的插入與刪除

string類中插入函數insert支持多種插入方式,有多個重載版本。

字符串刪除可以使用erease函數實現。

c++插入:
string& insert(int pos,const char *s);//從第pos位置開始插入s
string& insert(int pos,const string &s);

string &insert(int p0, const char *s, int n);//從p0位置開始插入s,插入的s連續n個字符
string &insert(int p0,const string &s, int pos, int n);//從p0位置開始插入s,插入的s從pos開始,連續n個字符

string &insert(int p0, int n, char c);//從p0處插入n個字符c

c++刪除字符串
string &erase(int pos,int n=npos);//從pos位置開始刪除n個字符

使用示例:

#include 
#include 
using namespace std;
void test()
{
	string str = "hello,";
	str.insert(2,"aaa");//從第2個位置開始插入aaa
	cout 
wKgZomSqxi6AVvZJAAKGeGuHEG4532.png

9.子字符串提取

string類中對于子字符串的提取,可以使用函數substr實現。

判斷string類字符串是否為空,即長度是否為0可以使用empty函數實現。

子字符串提取
string substr(int pos=0,int n=npos)const;//從pos位置提取n個子字符
//判斷字符串是否為空
bool empty();

使用示例:

wKgZomSqxp6AWpiFAAIBZEx4jZU077.png

10.string類轉換為char *

將string字符串轉換為char *字符串,可以使用c_str函數實現。

const char *c_str();
const char *data();
在c11版本之后,這兩個函數功能一致,都是將string類型字符串轉換成char *,返回一個char *字符串,以'?'結尾。

使用示例:

#include 
#include 
using namespace std;
int main()
{
	string str = "hello,world";
	const char* p = str.c_str();
	cout 
wKgZomSq33iAcHnsAAFBNkMtYHw370.png

審核編輯:湯梓紅

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 編程語言
    +關注

    關注

    9

    文章

    1880

    瀏覽量

    33380
  • 字符串
    +關注

    關注

    1

    文章

    552

    瀏覽量

    20173
  • C++
    C++
    +關注

    關注

    21

    文章

    2066

    瀏覽量

    72933
  • string
    +關注

    關注

    0

    文章

    40

    瀏覽量

    4687
收藏 人收藏

    評論

    相關推薦

    C語言字符串函數詳解

    , int c);查找字符c字符串string中首次出現的位置, NULL結束符也包含在查找中.返回一個指針, 指向
    發表于 06-23 23:33

    在Labivew里字符串String)轉為為字符(Char)數組。

    在Labivew里字符串String)轉為為字符(Char)。 我原來以為,這個函數應該有的,結果竟然沒有, 只要多動一番手腳了。如下圖:
    發表于 09-29 10:23

    Linux Shell系列教程之Shell字符串用法

    對于字符串的操作如下表格所示:[td]表達式含義${#string}$string的長度${string:position}在$string
    發表于 08-29 16:01

    如何實現字符串函數?

    你好 我使用STM8S發現套件和標準固件庫。我需要使用字符串函數,如strtok()。如何讓這些功能在這種環境中工作。包括string.h不會工作。我需要實現字符串函數還是有更簡單的方法?以上
    發表于 07-09 08:37

    LABVIEW 字符串變成字符數組

    怎么把一個字符串變成一個字符數組 ? 比如String "abc" 變成string[]{"a","b","c"}這種
    發表于 04-21 18:02

    字符串拆分

    串口連續傳來如是字符串”V-12.98C0.18“希望把字符串拆分為V后的為一組數,C后的為一組數。上述字符串應該拆為-12.98和0.18
    發表于 05-22 11:57

    請問JavaScript字符串對象String是什么?

    字符串對象 String 提供了對字符串進行處理的屬性和方法
    發表于 11-05 06:39

    string字符串和char*/char[]型型字符串的區別 相關資料分享

    概念區分在c中,是沒有string類型的數據的。但是c語言里有這個頭文件。容易讓人誤認為c中有string類型的數據。區分
    發表于 07-05 07:27

    LabVIEW編程LabVIEW開發 字符串對列String Queue PtByPt CYCk 例程與相關資料

    LabVIEW編程LabVIEW開發 字符串對列String Queue PtByPt CYCk 例程與相關資料Creates a string queue用于實現創建并保留最近的若干個字符串
    發表于 12-05 20:24

    字符串字符數組的轉換字符數組介紹

    字符串字符數組的轉換字符數組 -----》字符串1:直接在構造String時轉換。char[] data = {‘a’, ‘b’, ‘
    發表于 01-12 07:01

    C++字符串為什么被解釋為回退字符?

    我正在使用 TGX v4.16.0。我描述的所有工作都是使用 MSVS 中的 TGX 模擬器完成的。我正在從 C 字符數組構建本機 C++ 字符串。然后我將獲取這些
    發表于 01-04 06:38

    字符串string對象操作的全面總結

    ? ? 字符串操作看似簡單,其實非常重要,不注意的話,經常出現代碼運行結果和自己想要的不一致,甚至崩潰。本文總結了一些構建string對象方法、修改string對象的方法、string
    的頭像 發表于 11-11 11:23 ?1738次閱讀
    <b class='flag-5'>字符串</b><b class='flag-5'>string</b>對象操作的全面總結

    C++入門之string

    前一篇文章我們已經了解了C++中的基本類型,C++還提供了很多抽象數據類型,例如字符串string,string包含多個
    的頭像 發表于 03-17 13:58 ?374次閱讀

    鴻蒙TypeScript學習第10天:【String字符串)】

    String 對象用于處理文本(字符串)。
    的頭像 發表于 04-08 14:32 ?260次閱讀
    鴻蒙TypeScript學習第10天:【<b class='flag-5'>String</b>(<b class='flag-5'>字符串</b>)】
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>