C++中的String的常用函數(shù)用法
一. string的構(gòu)造函數(shù)的形式:
string str:生成空字符串
string s(str):生成字符串為str的復(fù)制品
string s(str, strbegin,strlen):將字符串str中從下標(biāo)strbegin開始、長度為strlen的部分作為字符串初值
string s(cstr, char_len):以C_string類型cstr的前char_len個(gè)字符串作為字符串s的初值
string s(num ,c):生成num個(gè)c字符的字符串
string s(str, stridx):將字符串str中從下標(biāo)stridx開始到字符串結(jié)束的位置作為字符串初值
eg:
string str1; //生成空字符串
string str2("123456789"); //生成"1234456789"的復(fù)制品
string str3("12345", 0, 3);//結(jié)果為"123"
string str4("012345", 5); //結(jié)果為"01234"
string str5(5, '1'); //結(jié)果為"11111"
string str6(str2, 2); //結(jié)果為"3456789"
#include <iostream>
#include <string>
using namespace std;
void test1()
{
string str1; //生成空字符串
string str2("123456789"); //生成"1234456789"的復(fù)制品
string str3("12345", 0, 3);//結(jié)果為"123"
string str4("0123456", 5); //結(jié)果為"01234"
string str5(5, '1'); //結(jié)果為"11111"
string str6(str2, 2); //結(jié)果為"3456789"
cout<<"str2:"<<str2<<endl;
cout<<"str3:"<<str3<<endl;
cout<<"str4:"<<str4<<endl;
cout<<"str5:"<<str5<<endl;
cout<<"str6:"<<str6<<endl;
}
int main()
{
test1();
return 0;
}
二. string的大小和容量::
1. size()和length():返回string對(duì)象的字符個(gè)數(shù),他們執(zhí)行效果相同。 2. max_size():返回string對(duì)象最多包含的字符數(shù),超出會(huì)拋出length_error異常 3. capacity():重新分配內(nèi)存之前,string對(duì)象能包含的最大字符數(shù)
void test2()
{
string s("1234567");
cout << "size=" << s.size() << endl;
cout << "length=" << s.length() << endl;
cout << "max_size=" << s.max_size() << endl;
cout << "capacity=" << s.capacity() << endl;
}
三. string的字符串比較:
1. C ++字符串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<”hello”)。 在使用>,>=,<,<=這些操作符的時(shí)候是根據(jù)“當(dāng)前字符特性”將字符按字典順序進(jìn)行逐一得 比較。字典排序靠前的字符小, 比較的順序是從前向后比較,遇到不相等的字符就按這個(gè)位置上的兩個(gè)字符的比較結(jié)果確定兩個(gè)字符串的大小(前面減后面) 同時(shí),string (“aaaa”) <string(aaaaa)。 2. 另一個(gè)功能強(qiáng)大的比較函數(shù)是成員函數(shù)compare()。他支持多參數(shù)處理,支持用索引值和長度定位子串來進(jìn)行比較。 他返回一個(gè)整數(shù)來表示比較結(jié)果,返回值意義如下:0:相等 1:大于 -1:小于 (A的ASCII碼是65,a的ASCII碼是97)
void test3()
{
// (A的ASCII碼是65,a的ASCII碼是97)
// 前面減去后面的ASCII碼,>0返回1,<0返回-1,相同返回0
string A("aBcd");
string B("Abcd");
string C("123456");
string D("123dfg");
// "aBcd" 和 "Abcd"比較------ a > A
cout << "A.compare(B):" << A.compare(B)<< endl; // 結(jié)果:1
// "cd" 和 "Abcd"比較------- c > A
cout << "A.compare(2, 3, B):" <<A.compare(2, 3, B)<< endl; // 結(jié)果:1
// "cd" 和 "cd"比較
cout << "A.compare(2, 3, B, 2, 3):" << A.compare(2, 3, B, 2, 3) << endl; // 結(jié)果:0
// 由結(jié)果看出來:0表示下標(biāo),3表示長度
// "123" 和 "123"比較
cout << "C.compare(0, 3, D, 0, 3)" <<C.compare(0, 3, D, 0, 3) << endl; // 結(jié)果:0
}
四. string的插入:push_back() 和 insert()
void test4()
{
string s1;
// 尾插一個(gè)字符
s1.push_back('a');
s1.push_back('b');
s1.push_back('c');
cout<<"s1:"<<s1<<endl; // s1:abc
// insert(pos,char):在制定的位置pos前插入字符char
s1.insert(s1.begin(),'1');
cout<<"s1:"<<s1<<endl; // s1:1abc
}
五、string拼接字符串:append() & + 操作符
void test5()
{
// 方法一:append()
string s1("abc");
s1.append("def");
cout<<"s1:"<<s1<<endl; // s1:abcdef
// 方法二:+ 操作符
string s2 = "abc";
/*s2 += "def";*/
string s3 = "def";
s2 += s3.c_str();
cout<<"s2:"<<s2<<endl; // s2:abcdef
}
六、 string的遍歷:借助迭代器 或者 下標(biāo)法
void test6()
{
string s1("abcdef"); // 調(diào)用一次構(gòu)造函數(shù)
// 方法一: 下標(biāo)法
for( int i = 0; i < s1.size() ; i++ )
{
cout<<s1[i];
}
cout<<endl;
// 方法二:正向迭代器
string::iterator iter = s1.begin();
for( ; iter < s1.end() ; iter++)
{
cout<<*iter;
}
cout<<endl;
// 方法三:反向迭代器
string::reverse_iterator riter = s1.rbegin();
for( ; riter < s1.rend() ; riter++)
{
cout<<*riter;
}
cout<<endl;
}
七、 string的刪除:erase()
1. iterator erase(iterator p);//刪除字符串中p所指的字符 2. iterator erase(iterator first, iterator last);//刪除字符串中迭代器 區(qū)間[first,last)上所有字符 3. string& erase(size_t pos = 0, size_t len = npos);//刪除字符串中從索引 位置pos開始的len個(gè)字符 4. void clear();//刪除字符串中所有字符
void test6()
{
string s1 = "123456789";
// s1.erase(s1.begin()+1); // 結(jié)果:13456789
// s1.erase(s1.begin()+1,s1.end()-2); // 結(jié)果:189
s1.erase(1,6); // 結(jié)果:189
string::iterator iter = s1.begin();
while( iter != s1.end() )
{
cout<<*iter;
*iter++;
}
cout<<endl;
}八、 string的字符替換:
1. string& replace(size_t pos, size_t n, const char *s);//將當(dāng)前字符串 從pos索引開始的n個(gè)字符,替換成字符串s 2. string& replace(size_t pos, size_t n, size_t n1, char c); //將當(dāng)前字符串從pos索引開始的n個(gè)字符,替換成n1個(gè)字符c 3. string& replace(iterator i1, iterator i2, const char* s);//將當(dāng)前字符串[i1,i2)區(qū)間中的字符串替換為字符串s
void test7()
{
string s1("hello,world!");
cout<<s1.size()<<endl; // 結(jié)果:12
s1.replace(s1.size()-1,1,1,'.'); // 結(jié)果:hello,world.
// 這里的6表示下標(biāo) 5表示長度
s1.replace(6,5,"girl"); // 結(jié)果:hello,girl.
// s1.begin(),s1.begin()+5 是左閉右開區(qū)間
s1.replace(s1.begin(),s1.begin()+5,"boy"); // 結(jié)果:boy,girl.
cout<<s1<<endl;
}九、 string的大小寫轉(zhuǎn)換:tolower()和toupper()函數(shù) 或者 STL中的transform算法
方法一:使用C語言之前的方法,使用函數(shù),進(jìn)行轉(zhuǎn)換
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "ABCDEFG";
for( int i = 0; i < s.size(); i++ )
{
s[i] = tolower(s[i]);
}
cout<<s<<endl;
return 0;
}方法二:通過STL的transform算法配合的toupper和tolower來實(shí)現(xiàn)該功能
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s = "ABCDEFG";
string result;
transform(s.begin(),s.end(),s.begin(),::tolower);
cout<<s<<endl;
return 0;
}十、 string的查找:find
1. size_t find (constchar* s, size_t pos = 0) const;
//在當(dāng)前字符串的pos索引位置開始,查找子串s,返回找到的位置索引,
-1表示查找不到子串
2. size_t find (charc, size_t pos = 0) const;
//在當(dāng)前字符串的pos索引位置開始,查找字符c,返回找到的位置索引,
-1表示查找不到字符
3. size_t rfind (constchar* s, size_t pos = npos) const;
//在當(dāng)前字符串的pos索引位置開始,反向查找子串s,返回找到的位置索引,
-1表示查找不到子串
4. size_t rfind (charc, size_t pos = npos) const;
//在當(dāng)前字符串的pos索引位置開始,反向查找字符c,返回找到的位置索引,-1表示查找不到字符
5. size_tfind_first_of (const char* s, size_t pos = 0) const;
//在當(dāng)前字符串的pos索引位置開始,查找子串s的字符,返回找到的位置索引,-1表示查找不到字符
6. size_tfind_first_not_of (const char* s, size_t pos = 0) const;
//在當(dāng)前字符串的pos索引位置開始,查找第一個(gè)不位于子串s的字符,返回找到的位置索引,-1表示查找不到字符
7. size_t find_last_of(const char* s, size_t pos = npos) const;
//在當(dāng)前字符串的pos索引位置開始,查找最后一個(gè)位于子串s的字符,返回找到的位置索引,-1表示查找不到字符
8. size_tfind_last_not_of (const char* s, size_t pos = npos) const;
//在當(dāng)前字符串的pos索引位置開始,查找最后一個(gè)不位于子串s的字符,返回找到的位置索引,-1表示查找不到子串void test8()
{
string s("dog bird chicken bird cat");
//字符串查找-----找到后返回首字母在字符串中的下標(biāo)
// 1. 查找一個(gè)字符串
cout << s.find("chicken") << endl; // 結(jié)果是:9
// 2. 從下標(biāo)為6開始找字符'i',返回找到的第一個(gè)i的下標(biāo)
cout << s.find('i',6) << endl; // 結(jié)果是:11
// 3. 從字符串的末尾開始查找字符串,返回的還是首字母在字符串中的下標(biāo)
cout << s.rfind("chicken") << endl; // 結(jié)果是:9
// 4. 從字符串的末尾開始查找字符
cout << s.rfind('i') << endl; // 結(jié)果是:18-------因?yàn)槭菑哪┪查_始查找,所以返回第一次找到的字符
// 5. 在該字符串中查找第一個(gè)屬于字符串s的字符
cout << s.find_first_of("13br98") << endl; // 結(jié)果是:4---b
// 6. 在該字符串中查找第一個(gè)不屬于字符串s的字符------先匹配dog,然后bird匹配不到,所以打印4
cout << s.find_first_not_of("hello dog 2006") << endl; // 結(jié)果是:4
cout << s.find_first_not_of("dog bird 2006") << endl; // 結(jié)果是:9
// 7. 在該字符串最后中查找第一個(gè)屬于字符串s的字符
cout << s.find_last_of("13r98") << endl; // 結(jié)果是:19
// 8. 在該字符串最后中查找第一個(gè)不屬于字符串s的字符------先匹配t--a---c,然后空格匹配不到,所以打印21
cout << s.find_last_not_of("teac") << endl; // 結(jié)果是:21
}十一、 string的排序:sort(s.begin(),s.end())
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void test9()
{
string s = "cdefba";
sort(s.begin(),s.end());
cout<<"s:"<<s<<endl; // 結(jié)果:abcdef
}十二、 string的分割/截取字符串:strtok() & substr()
strtok():分割字符串
void test10()
{
char str[] = "I,am,a,student; hello world!";
const char *split = ",; !";
char *p2 = strtok(str,split);
while( p2 != NULL )
{
cout<<p2<<endl;
p2 = strtok(NULL,split);
}
}
void test11()
{
string s1("0123456789");
string s2 = s1.substr(2,5); // 結(jié)果:23456-----參數(shù)5表示:截取的字符串的長度
cout<<s2<<endl;
}到此這篇關(guān)于C++中的String的常用函數(shù)用法的文章就介紹到這了,更多相關(guān)String的函數(shù)用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 類的賦值運(yùn)算符''''=''''重載的方法實(shí)現(xiàn)
這篇文章主要介紹了C++ 類的賦值運(yùn)算符'='重載的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
C語言中的fscanf()函數(shù)與vfscanf()函數(shù)使用
這篇文章主要介紹了C語言中的fscanf()函數(shù)與vfscanf()函數(shù)使用,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問題及解決方法
這篇文章主要介紹了解決Visual Studio Code運(yùn)行程序時(shí)輸出中文成亂碼問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C++實(shí)現(xiàn)俄羅斯方塊(windows API)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)俄羅斯方塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
linux c 獲得當(dāng)前進(jìn)程的進(jìn)程名和執(zhí)行路徑(示例)
如何得到當(dāng)前進(jìn)程的進(jìn)程名和執(zhí)行路徑。寫了個(gè)程序分享一下2013-07-07

