C++字元串操作函數:從不同角度操作字元串

一、字元串的定義和常用操作

C++中的字元串可以使用char數組或者string類實現。char數組是一組字元的集合,可以用字元串字面量或者字元數組初始化。string類則是一個預定義的類,可以直接使用字元串字面量或者字元串進行初始化。以下是常用的字元串操作函數:


#include <iostream>
#include <string>

int main() {
    char str1[] = "Hello";
    char str2[] = {'w', 'o', 'r', 'l', 'd', '\0'};
    std::string str3 = "C++";
    std::string str4 = "Programming";

    // 獲取字元串長度
    std::cout << "Length of str1: " << strlen(str1) << std::endl;
    std::cout << "Length of str2: " << strlen(str2) << std::endl;
    std::cout << "Length of str3: " << str3.length() << std::endl;
    std::cout << "Length of str4: " << str4.size() << std::endl;

    // 字元串拼接
    std::cout << "Concatenation of str1 and str2: " << strcat(str1, str2) << std::endl;
    std::cout << "Addition of str3 and str4: " << str3 + " " + str4 << std::endl;

    // 字元串複製
    char str5[10];
    strcpy(str5, str1);
    std::cout << "Copy of str1: " << str5 << std::endl;

    // 字元串查找
    std::cout << "Index of 'o' in str1: " << strchr(str1, 'o') - str1 + 1 << std::endl;
    std::cout << "Last index of 'l' in str1: " << strrchr(str1, 'l') - str1 + 1 << std::endl;
    std::cout << "Index of 'PP' in str3: " << str3.find("PP") + 1 << std::endl;

    return 0;
}

二、字元串的遍歷和比較

在進行字元串遍歷時,可以使用循環結構進行逐個遍歷,也可以使用string類的迭代器進行遍歷。字元串的比較可以使用比較運算符進行比較,也可以使用字元串操作函數進行比較。


#include <iostream>
#include <string>

int main() {
    std::string str1 = "This is a C++ string";
    std::string str2 = "C++ is a high-levle programming language";
    std::string str3 = "The quick brown fox jumps over the lazy dog";
    std::string str4 = "The quick brown fox jumps over the lazy dog";

    // 字元串遍歷 - 循環
    for (int i = 0; i < str1.length(); i++) {
        std::cout << str1[i] << " ";
    }
    std::cout << std::endl;

    // 字元串遍歷 - 迭代器
    for (std::string::iterator it = str2.begin(); it != str2.end(); it++) {
        std::cout << *it << "-";
    }
    std::cout << std::endl;

    // 字元串比較
    if (str3 == str4) {
        std::cout << "str3 is equal to str4" << std::endl;
    } else {
        std::cout << "str3 is not equal to str4" << std::endl;
    }

    if (str1.compare(str2) == 0) {
        std::cout << "str1 is equal to str2" << std::endl;
    } else {
        std::cout << "str1 is not equal to str2" << std::endl;
    }

    return 0;
}

三、字元串的截取和分割

在進行字元串截取時,可以使用substr方法,該方法接受兩個參數,第一個參數是開始截取的位置,第二個參數是截取的長度。在進行字元串分割時,可以使用getline方法,該方法接受兩個參數,第一個參數是輸入流,第二個參數是分隔符。通過循環使用getline方法,可以將字元串分割成多個子字元串。


#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main() {
    std::string str1 = "C++ is a high-level programming language";
    std::string str2 = "red,green,blue,yellow,black";

    // 字元串截取
    std::cout << "Substring of str1 from the 9th character for 5 characters: " << str1.substr(9, 5) << std::endl;
    std::cout << "Substring of str1 from the 0th character for the first space character: " << str1.substr(0, str1.find(" ")) << std::endl;

    // 字元串分割
    std::stringstream ss(str2);
    std::vector<std::string> vec;
    std::string temp;
    char delimiter = ',';

    while (getline(ss, temp, delimiter)) {
        vec.push_back(temp);
    }

    std::cout << "The original string: " << str2 << std::endl;
    std::cout << "The vector of substrings: ";
    for (auto x : vec) {
        std::cout << x << " ";
    }
    std::cout << std::endl;

    return 0;
}

四、字元串的插入和刪除

字元串的插入可以使用insert方法,該方法接受兩個參數,第一個參數是插入的位置,第二個參數是插入的字元串。字元串的刪除可以使用erase方法,該方法接受兩個參數,第一個參數是刪除的起始位置,第二個參數是刪除的長度。


#include <iostream>
#include <string>

int main() {
    std::string str1 = "This is a C++ string";
    std::string str2 = "Programming";
    std::string str3 = "C";

    // 字元串插入
    std::cout << "Inserting 'great' at position 10: " << str1.insert(10, " great") << std::endl;

    // 字元串刪除
    std::cout << "Erasing 7 characters starting from position 8: " << str2.erase(8, 7) << std::endl;

    // 刪除指定字元
    str3.erase(std::remove(str3.begin(), str3.end(), 'C'), str3.end());
    std::cout << "Removing all 'C' from str3: " << str3 << std::endl;

    return 0;
}

五、字元串的轉換和格式化

在進行字元串轉換時,可以使用atoi、atof等函數將字元串轉換成數字、浮點數等類型。在進行字元串格式化時,可以使用sprintf函數和C++11後的std::to_string方法進行格式化。


#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iomanip>

int main() {
    char str1[] = "12345";
    char str2[] = "3.1415";
    std::string str3 = "35";
    std::string str4 = "Hello";
    int num1 = atoi(str1);
    double num2 = atof(str2);
    int num3 = std::stoi(str3);
    double num4 = std::stod(str2);

    // 字元串轉換
    std::cout << "Converting str1 to an integer: " << num1 << std::endl;
    std::cout << "Converting str2 to a double: " << num2 << std::endl;
    std::cout << "Converting str3 to an integer: " << num3 << std::endl;
    std::cout << "Converting str2 to a double: " << num4 << std::endl;

    // 字元串格式化
    char buffer[50];
    sprintf(buffer, "%-.4f", num2);
    std::cout << "Formatting num2 to 4 decimal places: " << buffer << std::endl;
    std::string str5 = std::to_string(num2);
    std::cout << "Formatting num2 to 2 decimal places: " << std::setprecision(2) << std::fixed << num2 << std::endl;

    return 0;
}

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-20 00:17
下一篇 2024-11-20 00:17

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

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

    編程 2025-04-29
  • Python中capitalize函數的使用

    在Python的字元串操作中,capitalize函數常常被用到,這個函數可以使字元串中的第一個單詞首字母大寫,其餘字母小寫。在本文中,我們將從以下幾個方面對capitalize函…

    編程 2025-04-29
  • Python棧操作用法介紹

    如果你是一位Python開發工程師,那麼你必須掌握Python中的棧操作。在Python中,棧是一個容器,提供後進先出(LIFO)的原則。這篇文章將通過多個方面詳細地闡述Pytho…

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

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

    編程 2025-04-29
  • Python中set函數的作用

    Python中set函數是一個有用的數據類型,可以被用於許多編程場景中。在這篇文章中,我們將學習Python中set函數的多個方面,從而深入了解這個函數在Python中的用途。 一…

    編程 2025-04-29
  • 三角函數用英語怎麼說

    三角函數,即三角比函數,是指在一個銳角三角形中某一角的對邊、鄰邊之比。在數學中,三角函數包括正弦、餘弦、正切等,它們在數學、物理、工程和計算機等領域都得到了廣泛的應用。 一、正弦函…

    編程 2025-04-29
  • 單片機列印函數

    單片機列印是指通過串口或並口將一些數據列印到終端設備上。在單片機應用中,列印非常重要。正確的列印數據可以讓我們知道單片機運行的狀態,方便我們進行調試;錯誤的列印數據可以幫助我們快速…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變數時顯示的指定變數類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

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

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

    編程 2025-04-29

發表回復

登錄後才能評論