c++string字元串截取詳解

一、c截取字元串

c語言中可以使用strtok函數截取字元串,但是在c++中,可以使用string中的substr函數實現截取字元串。

substr函數接受兩個參數,第一個參數為截取的起始下標,第二個參數為要截取的長度。如果只傳入一個參數,則截取剩餘的全部字元串。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = str.substr(6, 5);
    cout << sub_str << endl;  // "world"
    return 0;
}

二、cstring截取字元串

cstring是c++中對於c語言中char*類型字元串的封裝。使用cstring截取字元串可以使用類似於c語言中的指針進行截取。

可以通過對於字元串名的取地址,來獲取字元串的首地址。利用指針可以直接操作字元串。

#include 
#include 
using namespace std;

int main() {
    char str[] = "hello world";
    char* sub_str = str + 6;
    cout << sub_str << endl;  // "world"
    return 0;
}

三、截取字元串string

在string中截取字元串也可以使用substr函數,其實現方式與c截取字元串類似。

需要注意的是,在實際使用中,可以採用string對象的方式截取字元串。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = string(str, 6, 5);
    cout << sub_str << endl;  // "world"
    return 0;
}

四、c截取字元串的指定字元

c語言中使用strchr函數來查找字元串中的指定字元,而在c++中可以使用find函數來實現。

find函數返回指定字元在字元串中第一次出現的位置。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    int index = str.find('w');
    string sub_str = str.substr(index);
    cout << sub_str << endl;  // "world"
    return 0;
}

五、std::string截取字元串

std::string和string的使用差不多,只是可能需要加上命名空間std::來使用。

std::string的substr函數用法和普通的string類似。

#include 
#include 
using namespace std;

int main() {
    std::string str = "hello world";
    std::string sub_str = str.substr(6, 5);
    cout << sub_str << endl;  // "world"
    return 0;
}

六、string截取指定字元串

使用string的find函數獲取子字元串在主字元串中的位置,再通過substr函數截取。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = "world";
    int index = str.find(sub_str);
    string result = str.substr(index);
    cout << result << endl;  // "world"
    return 0;
}

七、string類型截取字元串

可以使用string的find函數和substr函數實現截取字元串。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = "wo";
    int start = str.find(sub_str)+sub_str.size();
    string result = str.substr(start);
    cout << result << endl;  // "rld"
    return 0;
}

八、cstring截取到指定字元

和c語言中類似,使用strchr函數查找指定字元的位置,然後進行指針的操作實現字元串的截取。

#include 
#include 
using namespace std;

int main() {
    char str[] = "hello, world";
    char* sub_str = strchr(str, ',');
    *sub_str = '\0';
    cout << str << endl;  // "hello"
    return 0;
}

九、string截取兩個字元串之間的值

可以使用string的find_first_of和find_last_of函數查找兩個字元串的位置,然後通過substr函數進行截取。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string start_str = "h";
    string end_str = "d";
    int start = str.find_first_of(start_str)+1;
    int end = str.find_last_of(end_str);
    string result = str.substr(start, end-start);
    cout << result << endl;  // "ello worl"
    return 0;
}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242399.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:49
下一篇 2024-12-12 12:49

相關推薦

  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python中將字元串轉化為浮點數

    本文將介紹在Python中將字元串轉化為浮點數的常用方法。在介紹方法之前,我們先來思考一下這個問題應該如何解決。 一、eval函數 在Python中,最簡單、最常用的將字元串轉化為…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • Python學習筆記:去除字元串最後一個字元的方法

    本文將從多個方面詳細闡述如何通過Python去除字元串最後一個字元,包括使用切片、pop()、刪除、替換等方法來實現。 一、字元串切片 在Python中,可以通過字元串切片的方式來…

    編程 2025-04-29
  • c# enum轉換成string

    本文將從以下幾個方面詳細闡述c#中enum類型轉換成string類型的方法及注意事項。 一、基本語法和示例 c#中的enum類型可以看作是一組有名字的常量值,通常用於定義一組相關的…

    編程 2025-04-29
  • Python如何將字元串1234變成數字1234

    Python作為一種廣泛使用的編程語言,對於數字和字元串的處理提供了很多便捷的方式。如何將字元串「1234」轉化成數字「1234」呢?下面將從多個方面詳細闡述Python如何將字元…

    編程 2025-04-29
  • Python int轉二進位字元串

    本文將從以下幾個方面對Python中將int類型轉換為二進位字元串進行詳細闡述: 一、int類型和二進位字元串的定義 在Python中,int類型表示整數,二進位字元串則是由0和1…

    編程 2025-04-29
  • 用title和capitalize美觀處理Python字元串

    在Python中,字元串是最常用的數據類型之一。對字元串的美觀處理是我們在實際開發中經常需要的任務之一。Python內置了一些方法,如title和capitalize,可以幫助我們…

    編程 2025-04-28
  • Python 提取字元串中的電話號碼

    Python 是一種高級的、面向對象的編程語言,它具有簡單易學、開發迅速、代碼簡潔等特點,廣泛應用於 Web 開發、數據科學、人工智慧等領域。在 Python 中,提取字元串中的電…

    編程 2025-04-28
  • Python如何列印帶雙引號的字元串

    Python作為一種廣泛使用的編程語言,在日常開發中經常需要列印帶雙引號的字元串。那麼,如何列印帶雙引號的字元串呢? 一、使用轉義字元 在Python中,我們可以通過使用轉義字元\…

    編程 2025-04-28

發表回復

登錄後才能評論