boost-字符串處理-判斷-查找-裁剪-刪除-替換-分割-合并

文章目錄

    • 1.判斷
      • 1.1.equals
      • 1.2.all
      • 1.3.starts_with
      • 1.4.ends_with
      • 1.5.contains
    • 2.大小寫轉換
    • 3.字符串刪除
    • 4.字符串替換
    • 5.字符串查找
    • 6.字符串修剪
    • 7.字符串分割
    • 8.字符串合并
    • 9.總結

1.判斷

判別式函數和分類函數大多數都是以is_開頭,這些函數如下:
判別式函數包括:is_equal(等于)、is_less(小于)、is_not_greater(不大于)、lexicographical_compare(根據順序檢測一個字符串是否小于另一個)、starts_with(檢測一個字符串是否是另一個的前綴)、ends_with(檢測一個字符串是否是另一個的后綴)、contains(包含)、equals(相等)、all(檢測一個字符串中的所有元素是否滿足指定的判別式)。

分類函數:is_space、is_alnum(字符和數字)、is_alpha(字母)、is_cntrl(控制符)、is_digit(十進制)、is_graph(圖形字符)、is_lower(小寫)、is_upper(大寫)、is_print(可打印字符)、is_punct(標點符號)、is_xdigit(十六進制)、is_any_of(是否是序列中的任意字符)、if_from_range(是否位于指定區間,包括兩頭)

1.1.equals

#include <boost/algorithm/string.hpp>
assert(boost::equals("boost", "boost"));
assert(!boost::equals("boost", "BOOST"));
assert(boost::iequals("boost", "BOOST"));

1.2.all

all , 如果它的所有元素滿足一個給定的通過判斷式描述的條件,則這個條件式成立。

assert(boost::all("\x20\t\n\r", boost::is_space()));
assert(boost::all("\x20\t\n\r", boost::is_classified(std::ctype_base::space)));
assert(boost::all("\x20\t\n\r", boost::is_any_of("\x20\t\n\r")));
assert(boost::all("abcde", boost::is_from_range('a', 'e')));
assert(boost::all("abcde", boost::is_from_range('a', 'z')));
assert(!boost::all("abcde", boost::is_from_range('b', 'c')));
assert(boost::all("abc __ de", boost::is_from_range('a', 'z') || boost::is_space() || boost::is_any_of("_")));

1.3.starts_with

assert(boost::starts_with("boost_python-vc100-mt-1_49.dll", "boost"));
assert(!boost::starts_with("boost_python-vc100-mt-1_49.dll", "BOOST"));
assert(boost::istarts_with("boost_python-vc71-mt-1_33.dll", "BOOST"));

1.4.ends_with

assert(boost::ends_with("boost_python-vc100-mt-1_49.dll", ".dll"));
assert(!boost::ends_with("boost_python-vc100-mt-1_49.dll", ".DLL"));
assert(boost::iends_with("boost_python-vc100-mt-1_49.dll", ".DLL"));

1.5.contains

assert(boost::contains("boost_python-vc100-mt-1_49.dll", "python"));
assert(!boost::contains("boost_python-vc100-mt-1_49.dll", "PYTHON"));
assert(boost::icontains("boost_python-vc100-mt-1_49.dll", "PYTHON"));

is_space: 字符是否為空格
is_alnum: 字符是否為字母和數字字符
is_alpha: 字符是否為字母
is_cntrl: 字符是否為控制字符
is_digit: 字符是否為十進制數字
is_graph: 字符是否為圖形字符
is_lower: 字符是否為小寫字符
is_print: 字符是否為可打印字符
is_punct: 字符是否為標點符號字符
is_upper: 字符是否為大寫字符
is_xdigit: 字符是否為十六進制數字
is_any_of: 字符是否是參數字符序列中的任意字符
is_from_range 字符是否位于指定區間內,即from <= ch <= to

2.大小寫轉換

主要有如下API:
to_lower_copy:將原來字符串,轉換為小寫字符串,并返回新的字符串,原來字符串不改變。
to_upper_copy:將原來字符串,轉換為大寫字符串,并返回新的字符串,原來字符串不改變。
to_lower:將原來字符串,轉換為小寫字符串,原來字符串改變。
to_upper:將原來字符串,轉換為大寫字符串,原來字符串改變。

3.字符串刪除

boost庫提供了眾多的字符串刪除函數,并且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感、以_copy結尾的以不改變原來的字符串等,以滿足使用者不同的需求。

erase_first:刪除在字符串中第一個出現的字符串。
erase_last:刪除在字符串中最后一個出現的字符串。
erase_nth:刪除在字符串中第n個出現的字符串。
erase_all:刪除在字符串中所有出現的字符串。
erase_head:刪除輸入的開頭。
erase_tail:刪除輸入的結尾。

#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::string tmpStrErase = "Hello!Hello!I'm ISmileLi!Hello!Hello!I'm ISmileLi!";std::cout << "tmpStrErase:" << tmpStrErase << std::endl;boost::algorithm::erase_first(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::string tmpEraseLastStr = boost::algorithm::erase_last_copy(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::cout << "tmpEraseLastStr:" << tmpEraseLastStr << std::endl;boost::algorithm::erase_nth(tmpStrErase, "Hello", 2);std::cout << "tmpStrErase:" << tmpStrErase << std::endl;boost::algorithm::erase_all(tmpStrErase, "Hello");std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::string tmpEraseHeadCopyStr = boost::algorithm::erase_head_copy(tmpStrErase, 5);std::cout << "tmpStrErase:" << tmpStrErase << std::endl;std::cout << "tmpEraseHeadCopyStr:" << tmpEraseHeadCopyStr << std::endl;std::cout << "Hello World!\n";getchar();
}

4.字符串替換

boost庫提供了眾多的字符串替換函數,并且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感、以_copy結尾的以不改變原來的字符串等,以滿足使用者不同的需求。

boost庫提供的替換函數如下:
replace_first:替換在字符串中第一個出現的字符串。
replace_last:替換在字符串中最后一個出現的字符串。
replace_nth:替換在字符串中第n個出現的字符串。
replace_all:替換在字符串中所有出現的字符串。
replace_head:替換輸入的開頭。
replace_tail:替換輸入的結尾。

#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串替換------------------" << std::endl;std::string tmpStrReplace = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "-------------------打印原來字符串的值------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;boost::algorithm::replace_first(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替換第一個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::string tmpReplaceLastStr = boost::algorithm::replace_last_copy(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替換最后一個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::cout << "tmpReplaceLastStr:" << tmpReplaceLastStr << std::endl;boost::algorithm::replace_nth(tmpStrReplace, "HELLO", 2, "hello");std::cout << "-------------------替換第2個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;boost::algorithm::replace_all(tmpStrReplace, "HELLO", "hello");std::cout << "-------------------替換所有出現的字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::string tmpReplaceTailCopyStr = boost::algorithm::replace_tail_copy(tmpStrReplace, 5, "hello");std::cout << "-------------------替換結尾出現的幾個字符串后------------------" << std::endl;std::cout << "tmpStrReplace:" << tmpStrReplace << std::endl;std::cout << "tmpReplaceHeadCopyStr:" << tmpReplaceTailCopyStr << std::endl;std::cout << "Hello World!\n";getchar();
}

5.字符串查找

boost庫提供了眾多的字符串查找函數,并且提供了很多版本供使用,例如以i開頭的用來區分大小寫敏感的以不改變原來的字符串等,沒有_copy版本,以滿足使用者不同的需求。

boost庫提供的查找函數如下:
find_first:查找在字符串中第一個出現的字符串。
find_last:查找在字符串中最后一個出現的字符串。
find_nth:查找在字符串中第n個出現的字符串。
find_head:查找輸入的開頭第N個子串。
find_tail:查找輸入的結尾第N個子串。

#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串查找------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "-------------------打印原來字符串的值------------------" << std::endl;std::cout << "tmpStrFind:" << tmpStrFind << std::endl;boost::iterator_range<std::string::iterator> rIter = boost::algorithm::find_first(tmpStrFind, "ISmileLi");std::cout << "查找第一個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;//迭代器計算位置rIter = boost::algorithm::find_last(tmpStrFind, "ISmileLi");std::cout << "查找最后一個字符串ISmileLi出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;rIter = boost::algorithm::find_nth(tmpStrFind, "HELLO", 3);std::cout << "查找第3個字符串HELLO出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;rIter = boost::algorithm::find_head(tmpStrFind, 3);std::cout << "查找開頭3個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;std::cout << "rIter:" << rIter << std::endl;rIter = boost::algorithm::find_tail(tmpStrFind, 3);std::cout << "查找結尾3個字符串出現的位置: " << rIter.begin() - tmpStrFind.begin() << std::endl;std::cout << "rIter:" << rIter << std::endl;std::cout << "Hello World!\n";getchar();
}

6.字符串修剪

boost庫提供了眾多的字符串修剪函數,并且提供了很多版本供使用,例如以_copy、_if、_copy_if結尾的版本等,以滿足使用者不同的需求。主要函數有trim_left、trim_right、trim這三種方式以及它們的變種版本。

#include <iostream>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串修剪------------------" << std::endl;std::string tmpTrimSr = "  ...88HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!666...  ";std::cout << "-------------------打印原來字符串的值------------------" << std::endl;std::cout << "tmpTrimSr:" << tmpTrimSr << std::endl;std::cout << "-------------------刪除字符串空格------------------" << std::endl;std::string trimSpace = boost::algorithm::trim_copy(tmpTrimSr);std::cout << "不改變原字符串:" << trimSpace << std::endl;std::cout << "改變原字符串,刪除左邊:" << std::endl;boost::trim_left(tmpTrimSr);std::cout << tmpTrimSr << std::endl;std::cout << "改變原字符串,刪除右邊:" << std::endl;boost::trim_right(tmpTrimSr);std::cout << tmpTrimSr << std::endl;std::cout << "-------------------使用判別式刪除字符串兩端的空格、標點、數字------------------" << std::endl;std::cout << boost::trim_copy_if(tmpTrimSr, boost::is_space() || boost::is_punct() || boost::is_digit()) << std::endl;std::cout << "Hello World!\n";getchar();
}

7.字符串分割

boost庫提供了一個分割函數split(),可以根據某種特定的字符或者策略把分割后的字符,保存到指定的容器中。

#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串分割------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "原字符串:" << tmpStrFind << std::endl;std::vector<std::string> splitVector;// 使用標點符號切割字符串boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());std::cout << "-----------打印切割后的字符串----------" << std::endl;for (int i = 0; i < splitVector.size(); ++i){std::cout << splitVector.at(i) << std::endl;}// 常規用法std::string str = "Hello,World,How,Are,You";std::vector<std::string> result;boost::split(result, str, boost::is_any_of(","));std::cout << "Hello World!\n";getchar();
}

8.字符串合并

boost庫提供了一個合并函數join(),它可以把存儲在容器中的字符串通過指定的分隔符連接在一起。

#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>int main()
{std::cout << "-------------------boost庫字符串合并------------------" << std::endl;std::string tmpStrFind = "HELLO!HELLO!I'm ISmileLi!HELLO!HELLO!I'm ISmileLi!";std::cout << "原字符串:" << tmpStrFind << std::endl;std::vector<std::string> splitVector;// 使用標點符號切割字符串boost::algorithm::split(splitVector, tmpStrFind, boost::algorithm::is_punct());std::cout << "-----------打印切割后的字符串----------" << std::endl;for (int i = 0; i < splitVector.size(); ++i){std::cout << splitVector.at(i) << std::endl;}std::cout << "-----------使用合并函數join并打印合并后的字符串----------" << std::endl;std::cout << boost::algorithm::join(splitVector, "+") << std::endl;//lambda表達式std::string tempJoinIfStr = boost::algorithm::join_if(splitVector, "$$", [](std::string tmpStr) {return boost::algorithm::contains(tmpStr, "I");});std::cout << "tempJoinIfStr: " << tempJoinIfStr << std::endl;std::cout << "Hello World!\n";getchar();
}

9.總結

字符串的常用操作,軟件工程師使用起來,還是非常方便快捷。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/208889.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/208889.shtml
英文地址,請注明出處:http://en.pswp.cn/news/208889.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ElasticSearch之線程池

ElasticSearch節點可用的CPU核的數量&#xff0c;通常可以交給ElasticSearch來自行檢測和判定&#xff0c;另外可以在elasticsearch.yml中顯式指定。樣例如下&#xff1a; node.processors: 2如下表格中的processors即CPU核的數量。 線程池的列表 線程池名稱類型線程數量隊列…

屏蔽百度首頁推薦和熱搜的實戰方案

大家好,我是愛編程的喵喵。雙985碩士畢業,現擔任全棧工程師一職,熱衷于將數據思維應用到工作與生活中。從事機器學習以及相關的前后端開發工作。曾在阿里云、科大訊飛、CCF等比賽獲得多次Top名次。現為CSDN博客專家、人工智能領域優質創作者。喜歡通過博客創作的方式對所學的…

電視節目中活動滅燈系統是如何實現的

活動滅燈系統主要用于各種需要亮燈或滅燈的活動節目&#xff0c;如招聘滅燈、相親滅燈等。有多種燈光顏色供選擇&#xff0c;本設備通過按鈕燈軟件組合實現&#xff0c;用戶可以自己設置亮燈或滅燈規則。 軟件功能&#xff1a; 1、后臺統一控制亮燈&#xff0c;重新開始下輪…

華為交換機基本配置

一、配置時間 sys ntp-service unicast-server 192.168.1.1 ntp-service unicast-server 192.168.1.2 clock timezone UTC add 8 clock timezone CST add 08:00:00 undo ntp-service disable q手動設置一個時間 clock datetime 13:43:00 2023-10-10save ysys保存&#xff01;保…

某60內網滲透之域管權限維持[金票利用]

內網滲透 文章目錄 內網滲透域管權限維持【金票利用】實驗目的實驗環境實驗工具實驗原理實驗內容域管權限維持【金票利用】實驗步驟攻擊域管權限維持【金票利用】 實驗目的 讓學員通過該系統的練習主要掌握:利用金票來維持域管理員的權限。 實驗環境 操作機 Windows 7,域…

微信小程序 - 格式化操作 moment.js格式化常用使用方法總結大全

格式化操作使用 1. 首先&#xff0c;下載一個第三方庫 moment npm i moment --save 注&#xff1a;在微信小程序中無法直接npm 下載 導入 的&#xff08;安裝一個就需要構建一次&#xff09; 解決&#xff1a;菜單欄 --> 工具 --> 構建 npm 點擊即可&#xff08;會…

線性回歸模型標準公式

用一組特征 x ( i ) { x^{(i)}} x(i)來預測或估計一個響應變量 y ( i ) y^{(i)} y(i)&#xff0c;公式如下&#xff1a; y ( i ) θ T x ( i ) ? ( i ) y^{(i)} \theta^T x^{(i)} \epsilon^{(i)} y(i)θTx(i)?(i) 各名詞解釋&#xff1a; y ( i ) y^{(i)} y(i)&#xf…

Docker import 命令

docker import&#xff1a;從歸檔文件中創建鏡像。 語法&#xff1a; docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]OPTIONS說明&#xff1a; -c &#xff1a;應用docker指令創建鏡像&#xff1b; -m &#xff1a;提交時的說明文字&#xff1b; 實例&#xff1a…

蝦皮免費分析工具:了解市場趨勢、優化產品和店鋪運營

在如今競爭激烈的電商市場中&#xff0c;了解市場趨勢、優化產品和店鋪運營對于賣家來說至關重要。蝦皮&#xff08;Shopee&#xff09;作為一家知名的電商平臺&#xff0c;為賣家提供了一些免費的分析工具&#xff0c;幫助他們更好地了解市場情況并做出明智的決策。本文將介紹…

C/C++,優化算法——雙離子推銷員問題(Bitonic Travelling Salesman Problem)的計算方法與源代碼

1 文本格式 // C program for the above approach #include <bits/stdc.h> using namespace std; // Size of the array a[] const int mxN 1005; // Structure to store the x and // y coordinates of a point struct Coordinates { double x, y; } a[mxN]; //…

[架構之路-259]:目標系統 - 設計方法 - 軟件工程 - 軟件設計 - 架構設計 - 面向服務的架構SOA與微服務架構(以服務為最小的構建單位)

目錄 前言&#xff1a; 二、軟件架構層面的復用 三、什么是面向服務的架構SOA 3.1 什么是面向服務的架構 3.2 面向服務架構的案例 3.3 云服務&#xff1a;everything is service一切皆服務 四、什么是微服務架構 4.1 什么是微服務架構 4.2 微服務架構的案例 五、企業…

樹莓派 5 - Raspberry Pi 5 入門教程

系列文章目錄 文章目錄 ??????? 前言 如果您是第一次使用 Raspberry Pi&#xff0c;請參閱我們的入門指南&#xff08;how to get started&#xff09;。 Raspberry Pi 5 Raspberry Pi 5 配備了運行頻率為 2.4GHz 的 64 位四核 Arm Cortex-A76 處理器&#xff0c;CPU 性…

java第三十三課

ISBN 編號&#xff1a;字符串 商品模塊中&#xff1a;增刪改查是最基本的操作。 查詢&#xff1a;復雜查詢&#xff08;與多表有關系&#xff09; 訂單&#xff0c;訂單詳情兩個表 訂單&#xff08;增刪改查&#xff09;&#xff0c; 訂單詳情&#xff08;增刪改查&#xff09;…

LangChain+通義千問+AnalyticDB向量引擎保姆級教程

本文以構建AIGC落地應用ChatBot和構建AI Agent為例&#xff0c;從代碼級別詳細分享AI框架LangChain、阿里云通義大模型和AnalyticDB向量引擎的開發經驗和最佳實踐&#xff0c;給大家快速落地AIGC應用提供參考。 前言 通義模型具備的能力包括&#xff1a; 1.創作文字&#xf…

【已解決】SpringBoot Maven 打包失敗:class lombok.javac.apt.LombokProcessor 錯誤

文章目錄 出錯原因解決辦法總結 最新項目部署的時候&#xff0c;出現了一個maven打包失敗的問題&#xff0c;主要是lombok這個組件出的問題&#xff0c;具體的錯誤信息如下&#xff1a; 我的lombok版本如下&#xff1a; <dependency><groupId>org.projectlombok&l…

Android View.inflate 和 LayoutInflater.from(this).inflate 的區別

前言 兩個都是布局加載器&#xff0c;而View.inflate是對 LayoutInflater.from(context).inflate的封裝&#xff0c;功能相同&#xff0c;案例使用了dataBinding。 View.inflate(context, layoutResId, root) LayoutInflater.from(context).inflate(layoutResId, root, fals…

【JS】JS數組添加元素的三種方法

> 1、push() 方法可向數組的末尾添加一個或多個元素&#xff0c;并返回新的長度。 > 2、unshift()方法可向數組的開頭添加一個或更多元素&#xff0c;并返回新的長度。 > 3、splice() 方法向/從數組中添加/刪除項目&#xff0c;然后返回被刪除的項目。1、push() 方法…

nodejs+vue+微信小程序+python+PHP的黃山旅游景點購票系統設計與實現-計算機畢業設計推薦

本文首先對該系統進行了詳細地描述&#xff0c;然后對該系統進行了詳細的描述。管理人員增加了系統首頁、個人中心、用戶管理、景點分類管理、景點簡介管理、旅游路線管理、文章分類管理、公告文章管理、系統管理理等功能。黃山旅游景點購票系統是根據當前的現實需要&#xff0…

線程池的原理和基本使用~

線程池的基本原理&#xff1a; 無論是之前在JavaSE基礎中&#xff0c;我們學習過的常量池&#xff0c;還是在操作數據庫時&#xff0c;我們學習過數據庫連接池&#xff0c;以及接下來要學習的線程池&#xff0c;均是一種池化思想&#xff0c;其目的就是為了提高資源的利用率&a…

mysql 鏈接超時的幾個參數詳解

mysql5.7版本中&#xff0c;先查看超時設置參數&#xff0c;我們這里只關注需要的超時參數&#xff0c;并不是全都講解 show variables like %timeout%; connect_timeout 指的是連接過程中握手的超時時間,在5.0.52以后默認為10秒&#xff0c;之前版本默認是5秒&#xff0c;主…