string類的用法總結


string中常見的成員函數


示例代碼:

string s= string("abcdefg");
char ch[] = "abcdefgd";

//調用構造函數全部復制
string str1 = string(s);

//構造函數,從字符串str的第2個元素開始,復制5個元素,賦值給str2
string str2 = string(s,2,5);

//復制ch前5個字符串
string str3 = string(ch,5);

//將5個'6'賦給字符串
string str4 = string(5,'6');

//
string str5 = string(s.begin(),s.end());

cout << "oringin s = " + s << endl;
cout << "oringin ch = " <<ch << endl;
cout << "str1 = " + str1<< endl;
cout << "str2 = " + str2<<endl;
cout << "str3 = " + str3<<endl;
cout << "str4 = " + str4<<endl;
cout << "str5 = " + str5<<endl;

輸出結果:

string的常用方法

取string中元素

使用at()或[]去字符串的元素

string s = "hello";
char c = s.at(2);
char ch = s[2];

上面兩行的效果是一樣的,都是將l取出。

字符串比較

  1. compare函數
    函數原型如下所示:

    int compare (const basic_string& s) const;
    int compare (const Ch* p) const;
    int compare (size_type pos, size_type n, const basic_string& s) const;
    int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
    int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;

函數返回值介紹:若參與比較的兩個串值相同,則函數返回 0;若字符串 S 按字典順序要先於 S2,則返回負值;反之,則返回正值。下面舉例說明如何使用 string 類的 compare() 函數。

具體示例如下:

string s1 = "Hello";
string s2 = "Ok";
string s3 = "abdcds";
string s4("abdcds");

cout << "s1 = " + s1 <<endl;
cout << "s2 = " + s2 <<endl;
cout << "s3 = " + s3 <<endl;
cout << "s4 = " + s4 <<endl;

cout<<"s1.comapre(s2) = " << s1.compare(s2) <<endl;
cout<<"s2.comapre(s3) = " << s2.compare(s3) <<endl;
cout<<"s3.comapre(s4) = " << s3.compare(s4) <<endl;

輸出結果如下:

s1 = Hello
s2 = Ok
s3 = abdcds
s4 = abdcds
s1.comapre(s2) = -1
s2.comapre(s3) = -1
s3.comapre(s4) = 0
  1. 比較運算符
    String 類的常見運算符包括 >、<、==、>=、<=、!=。其意義分別為"大於"、"小於"、"等於"、"大於等於"、"小於等於"、"不等於"

示例用法:

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

void TrueOrFalse (int x)
{
cout << (x?"True":"False")<<endl;
}

int main ()
{
string S1 = "DEF";
string CP1 = "ABC";
string CP2 = "DEF";
string CP3 = "DEFG";
string CP4 ="def";
cout << "S1= " << S1 << endl;
cout << "CP1 = " << CP1 <<endl;
cout << "CP2 = " << CP2 <<endl;
cout << "CP3 = " << CP3 <<endl;
cout << "CP4 = " << CP4 <<endl;
cout << "S1 <= CP1 returned ";
TrueOrFalse (S1 <=CP1);
cout << "S1 <= CP2 returned ";
TrueOrFalse (S1 <= CP2);
cout << "S1 <= CP3 returned ";
TrueOrFalse (S1 <= CP3);
cout << "CP1 <= S1 returned ";
TrueOrFalse (CP1 <= S1);
cout << "CP2 <= S1 returned ";
TrueOrFalse (CP2 <= S1);
cout << "CP4 <= S1 returned ";
TrueOrFalse (CP4 <= S1);
cin.get();
return 0;
}

輸出結果:

S1= DEF
CP1 = ABC
CP2 = DEF
CP3 = DEFG
CP4 = def
S1 <= CP1 returned False
S1 <= CP2 returned True
S1 <= CP3 returned True
CP1 <= S1 returned True
CP2 <= S1 returned True
CP4 <= S1 returned False

字符串內容的修改

  1. 使用append()函數

    basic_string& append (const E * s); //在原始字符串后面追加字符串s
    basic_string& append (const E * s, size_type n);//在原始字符串后面追加字符串 s 的前 n 個字符
    basic_string& append (const basic_string& str, size_type pos,size_type n);//在原始字符串后面追加字符串 s 的子串 s [ pos,…,pos +n -1]
    basic_string& append (const basic_string& str);
    basic_string& append (size_type n, E c); //追加 n 個重復字符
    basic_string& append (const_iterator first, const_iterator last); //使用迭代器追加

示例代碼如下:

string s1 = "123456";
string s2 = "abcdefgh";

string str;

str.assign(s1);
cout <<"after use assign: str = " << str << endl;
  1. 使用insert函數

    basic_string& insert (size_type p0 , const E * s); //插人 1 個字符至字符串 s 前面
    basic_string& insert (size_type p0 , const E * s, size_type n); // 將 s 的前 3 個字符插入p0 位置
    basic_string& insert (size_type p0, const basic_string& str);
    basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //選取 str 的子串
    basic_string& insert (size_type p0, size_type n, E c); //在下標 p0 位置插入 n 個字符 c
    iterator insert (iterator it, E c); //在 it 位置插入字符 c
    void insert (iterator it, const_iterator first, const_iterator last); //在字符串前插入字符
    void insert (iterator it, size_type n, E c) ; //在 it 位置重復插入 n 個字符 c

示例代碼如下:

string A("ello");
string B ;
B.insert(1,A);
cout << B << endl;
A = "ello";
B = "H";
B.insert (1,"yanchy ",3);
cout<< B <<endl;
A = "ello";
B = "H";
B.insert (1,A,2,2);
cout << B << endl;
A="ello";
B.insert (1 , 5 , 'C');
cout << B << endl;

字符串替換函數

方法原型:

basic_string& replace (size_type p0, size_type n0, const E * s); //使用字符串 s 中的 n 個字符,從源串的位置 P0 處開始替換
basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字符串 s 中的 n 個字符,從源串的位置 P0 處開始替換 1 個字符
basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字符串 s 中的 n 個字符,從源串的位置 P0 處開始替換
basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替換源串中的內容,從位置 p0 處開                            
始替換,替換字符 n0 個
basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 個字符 'c' 替換源串中位置 p0 處開始的 n0 個字符
basic_string& replace (iterator first0, iterator last0, const E * s);//使用迭代器替換,和 1) 用法類似
basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 類似
basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 類似
basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 類似
basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替換

示例代碼如下:

#include <iostream>
#include <string>
using namespace std;
int main ()
{
string var ("abcdefghijklmn");
const string dest ("1234");
string dest2 ("567891234");
var.replace (3,3, dest);
cout << "1: " << var << endl;
var = "abcdefghijklmn";
var.replace (3,1, dest.c_str(), 1, 3);
cout << "2: " << var << endl;
var ="abcdefghijklmn";
var.replace (3, 1, 5, 'x');
cout << "3: " << var << endl;
string::iterator itA, itB;
string::iterator itC, itD;
itA = var.begin();
itB = var.end();
var = "abcdefghijklmn";
var.replace (itA, itB, dest);
cout << "4: " << var << endl;
itA = var.begin ();
itB = var.end();
itC = dest2.begin () +1;
itD = dest2.end ();
var = "abodefghijklmn";
var.replace (itA, itB, itC, itD);
cout << "5: " << var << endl;
var = "abcdefghijklmn";
var.replace (3, 1, dest.c_str(), 4); //這種方式會限定字符串替換的最大長度
cout <<"6: " << var << endl;
return 0;
}

輸出結果 :

1: abc1234ghijklmn
2: abc234efghijklmn
3: abcxxxxxefghijklmn
4: 1234
5: 67891234efghijklmn
6: abc1234efghijklmn

字符串查找find


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM