c++字符串截取函數?1.1 單字節字符集(single-byte character set(SBCS)),今天小編就來聊一聊關于c++字符串截取函數?接下來我們就一起去研究一下吧!
1.1 單字節字符集(single-byte character set(SBCS))
顧名思義,在這種模式下,所有的字符都隻用一個字節表示,常見的如,ASCII
1.2 多字節字符集(multi-byte character set(MBCS))
在Windows中MBCS包含兩種字符類型,單字節字符和雙字節字符.由于windows使用的多字節字符絕大部分是兩個字節長,所以MBCS常被DBCS代替。
1.3 unicode
Unicode是一種所有的字符都使用兩個字節編碼的編碼模式。Unicode字符有時也被稱作寬字符,因為它比單子節字符寬(使用了更多的存儲空間)。
常見的為utf-8,還有一個比較少用的是utf-16
2.1 C中的字符串
C語言中, 沒有字符串的數據類型,使用一個以NULL(‘\0’)字符結尾的字符數組來保存字符串。
char* p = “Hello world”;
char* p = (char*)malloc (100 * sizeof(char));
char p[100] = “hello world”;
2.2 C++ 中的字符串
C++ 語言中有單獨字符串類型,在string頭文件中,還有對應的寬字符串wstring.
string iStr = “Hello world”;
wstring wStr = “Hello world”;
3.1 構造方法
string(const char *s); //用c字符串s初始化
string(int n,char c); //用n個字符c初始化
此外,string類還支持默認構造函數和複制構造函數,如string s1;string s2=”hello”;都是正确的寫法。當構造的string太長而無法表達時會抛出length_error異常
3.2 操作方法
3.2.1 字符操作
const char &operator[](int n)const; // 索引操作,取單個字符
const char &at(int n)const;// 同上
char &operator[](int n);//同上
char &at(int n);//同上
operator[]和at()均返回當前字符串中第n個字符的位置,但at函數提供範圍檢查,當越界時會抛出out_of_range異常,下标運算符[]不提供檢查訪問。
const char *data()const;//返回一個非null終止的c字符數組
const char *c_str()const;//返回一個以null終止的c字符串
int copy(char *s, int n, int pos = 0) const;//把當前串中以pos開始的n個字符拷貝到以s為起始位置的字符數組中,返回實際拷貝的數目
3.2.2 string的特性描述
int capacity()const; //返回當前容量(即string中不必增加内存即可存放的元素個數)
int max_size()const; //返回string對象中可存放的最大字符串的長度
int size()const; //返回當前字符串的大小
int length()const; //返回當前字符串的長度
bool empty()const; //當前字符串是否為空
void resize(int len,char c);//把字符串當前大小置為len,并用字符c填充不足的部分
3.2.3 string類的輸入輸出操作
string類重載運算符operator>>用于輸入,同樣重載運算符operator<<用于輸出操作。
函數getline(istream &in,string &s);用于從輸入流in中讀取字符串到s中,以換行符’\n’分開。
3.2.4 string的賦值
string &operator=(const string &s);//把字符串s賦給當前字符串
string &assign(const char *s);//用c類型字符串s賦值
string &assign(const char *s,int n);//用c字符串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個字符賦給當前字符串
string &assign(const_iterator first,const_itertor last);//把first和last叠代器之間的部分賦給字符串
3.2.5 string的連接
string &operator+=(const string &s);//把字符串s連接到當前字符串的結尾
string &append(const char *s); //把c類型字符串s連接到當前字符串結尾
string &append(const char *s,int n);//把c類型字符串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
string &append(const_iterator first,const_iterator last);//把叠代器first和last之間的部分連接到當前字符串的結尾
3.2.6 string的比較
bool operator==(const string &s1,const string &s2)const;//比較兩個字符串是否相等
運算符”>”,”<“,”>=”,”<=”,”!=”均被重載用于字符串的比較;
int compare(const string &s) const;//比較當前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比較當前字符串從pos開始的n個字符組成的字符串與s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符組成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函數在>時返回1,<時返回-1,==時返回0
3.2.7 string的子串
string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字符組成的字符串
3.2.8 string的交換
void swap(string &s2); //交換當前字符串與s2的值
3.2.9 string類的查找函數
int find(char c, int pos = 0) const;//從pos開始查找字符c在當前字符串的位置
int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當前串中的位置
int find(const string &s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
//查找成功時返回所在位置,失敗返回string::npos的值
int rfind(char c, int pos = npos) const;//從pos開始從後向前查找字符c在當前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//從pos開始從後向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值
int find_first_of(char c, int pos = 0) const;//從pos開始查找字符c第一次出現的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//從pos開始查找當前串中第一個在s的前n個字符組成的數組裡的字符的位置。查找失敗返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//從當前串中查找第一個不在串s中的字符出現的位置,失敗返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,隻不過是從後向前查找
3.2.10 string類的替換函數
string &replace(int p0, int n0,const char *s);//删除從p0開始的n0個字符,然後在p0處插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0開始的n0個字符,然後在p0處插入字符串s的前n個字符
string &replace(int p0, int n0,const string &s);//删除從p0開始的n0個字符,然後在p0處插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0開始的n0個字符,然後在p0處插入串s中從pos開始的n個字符
string &replace(int p0, int n0,int n, char c);//删除p0開始的n0個字符,然後在p0處插入n個字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換為字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換為s的前n個字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換為串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之間的部分替換為n個字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字符串
3.2.11 string類的插入函數
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4個函數在p0位置插入字符串s中pos開始的前n個字符
string &insert(int p0, int n, char c);//此函數在p0處插入n個字符c
iterator insert(iterator it, char c);//在it處插入字符c,返回插入後叠代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字符
void insert(iterator it, int n, char c);//在it處插入n個字符c
3.2.12 string類的删除函數
iterator erase(iterator first, iterator last);//删除[first,last)之間的所有字符,返回删除後叠代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除後叠代器的位置
string &erase(int pos = 0, int n = npos);//删除pos開始的n個字符,返回修改後的字符串
3.2.13 string類的叠代器處理
string類提供了向前和向後遍曆的叠代器iterator,叠代器提供了訪問各個字符的語法,類似于指針操作,叠代器不檢查範圍。
用string::iterator或string::const_iterator聲明叠代器變量,const_iterator不允許改變叠代的内容。常用叠代器函數有:
const_iterator begin()const;
iterator begin(); //返回string的起始位置
const_iterator end()const;
iterator end(); //返回string的最後一個字符後面的位置
const_iterator rbegin()const;
iterator rbegin(); //返回string的最後一個字符的位置
const_iterator rend()const;
iterator rend(); //返回string第一個字符位置的前面
rbegin和rend用于從後向前的叠代訪問,通過設置叠代器string::reverse_iterator,string::const_reverse_iterator實現
需要學習的加C語言C++學習交流群 496926338
關于學習态度
學習嘛,在學習的過程中自然會遇到一些問題,遇到問題自然會想到求助。古人有雲:禮下于人,必有所求,言下之意是有求于人,必要禮下于人。可是現在有些人不僅不禮下于人,反而還趾高氣揚,仿佛誰欠着他似的。我們老家有句俗話叫拜師學藝要吃二十四碗鼻涕,可見在學習過程中要受到多少的委屈。
吃得苦中苦,方為人上人,做技術尤是如此。我以前在學ASP的時候,同寝室有一哥們是學VB的,因為ASP也是用VBScript做為默認腳本,所以有很多問題請教他,他當是也是給了我不少的嘲笑,像“這些簡單的問題都還要問”之類的話我倒是聽了不少。但到後我們一起學Delphi的時候,我就已經超過他了。
新人們碰到問題的時候,第一個想法就是想找人解決,當你去幫他解決的時候,他卻去做其它事了,好像這個問題是需要你來解決的,下次出現同樣的問題他還得找你。遇到問題最好的辦法是先排查問題,然後到互聯網上查找問題的原因,這樣你才不會忘記,不要忽略了互聯網這個最好的老師。我以前在學ASP的時候,為了一個插入數據的問題,查了一個星期,但這個問題我永遠也忘記不了,再也不會範同樣的錯誤。
高手們一般都比較忙,雖然我不是高手,但我還是不原意在我寫代碼正酣的時候,有人打斷我的思路。先把問題記下來,趁機會問問高手,不要讓他為你解決問題,你要的是一種解決問題的思路。公司常常有新人問我,這個地方出問題了,怎麼辦?我便問他,你上網查了嗎?不是我不願意幫他,而是授人魚不如授人以漁,我直接告訴他怎麼做,那不是在幫他,那是我在害他,我隻能是告訴他排錯的方法,解決問題的
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!