最實用的的c++中string函式的用法,沒有之一。
純屬原創,
String函式的用法
string的定義及初始
string s1 = "hello"; //初始化字串
string s2 ("world"); //另一種初始化
string s3; //初始化字串,空字串
string s4(5, 'a'); //s4由連續5個a組成,即s4="aaaaa";
string s5(s1,2,3); //從s1的2位置的字元開始,連續3個字元賦值給s5,即s5="llo";
string s6(s1, 1); //從s1的2位置的字元開始,將後續的所有字元賦值給s6,即s6="ello";
s1 = "s" + s2 + "s" + "s"; //正確,“+”的兩邊要保證至少有一個string型別string+“s”。
Char型別陣列轉化成String型別
假設c字串定義為char ch[]="hello world!";
1.向建構函式傳入c字串建立string物件:
string str(ch);
2.使用拷貝建構函式建立string物件:
string str = ch;
3.對已有的string物件呼叫string類內部定義的賦值運算子:
string str;
str = ch;
前兩種類似,但和第三種有較大區別,前兩種是運用建構函式直接建立一個內容與c字串一致的string物件;第三種是c++標準庫編寫的string類的內部過載了賦值運算子,使之能夠以c字串作為右運算元對string物件進行賦值,使string物件的內容與c字串一致。
字串擷取
用法1:擷取下標從2(第3個字元)開始到字串結尾的字串
string str = "ABCDEFG";
string cut= str.substr(2);
最終,cut="CDEFG"。
衍生:如果str="image007.jpg",而我們想知道其副檔名(filename extension),那麼可以這樣操作:
string str = "image007.jpg";
string cut= str.substr(str.find_last_of(".")+1);
最終,cut="jpg",得到副檔名。其中,str.find_last_of(".")返回str字串中最後一個'.'的所在下標,這裡返回8(int)。
用法2:擷取下標從2(第3個字元)開始擷取3個字元的字串
string str = "ABCDEFG";
string cut= str.substr(2,3)
最終,cut="CDE",即從下標為2開始向後數3位。
衍生:如果str="image007.jpg",而我們只要其檔名而不要副檔名,那麼可以這樣操作:
string str = "image007.jpg";
string cut= str.substr(0,str.find_last_of("."));
最終,cut="image007",得到不含副檔名的檔名。
String 中的查詢
string str1 = "cup,car,person,car,booo";
string str2 = "ako";
int num_1 = str1.find_first_of(str2);//返回str1中第一個與str2的第一個字元('a')相同字元的下標 ,返回5
int num_2 = str1.find_first_not_of(str2);//返回str1中第一個與str2的第一個字元('a')不同字元的下標 ,返回0
int num_3 = str1.find_last_of(str2);//返回str1中最後一個與str2的最後一個字元('o')相同字元的下標 ,返回22
int num_4 = str1.find_last_not_of(str2);//返回str1中最後一個與str2的最後一個字元('o')不同字元的下標 ,返回19
詳細:
s = "asdfg";
n = s.find("asdfg"); //n=0 查詢 全匹配
n=s.find(100); //n=2,將100轉為字元查詢,d的ascii碼為100
n=s.find('dfg'); //n=4
n = s.find("dfg"); //n=2
n=s.find("dhh"); //n=string::npos=4294967295
n = s.find("wer"); //n=string::npos=4294967295
n=s.find('f'); //n=3
n = s.find("f"); //n=3
char as = 'dfg'; //as='g'
n = s.find_first_of("asdfg"); //n=0,查詢包含子串中的任何字元, 返回第一個位置
n = s.find_first_of(3); //n=string::npos=4294967295
n = s.find_first_of('dfg'); //n=4
n = s.find_first_of("dfg"); //n=2
n = s.find_first_of("dhh"); //n=2
n = s.find_first_of("wer"); //n=string::npos=4294967295
n = s.find_first_of('f'); //n=3,從pos開始查詢字元第一次出現的位置
n = s.find_first_of("f"); //n=3
n = s.find_last_of("asdfg"); //n=4,查詢包含子串中的任何字元,返回最後一個位置
n = s.find_last_of(3); //n=string::npos=4294967295
n = s.find_last_of('dfg'); //n=4
n = s.find_last_of("dfg"); //n=4
n = s.find_last_of("dhh"); //n=2
n = s.find_last_of("wer"); //n=string::npos=4294967295
n = s.find_last_of('f'); //n=3
n = s.find_last_of("f"); //n=3
n = s.find_first_not_of("asdfg");//n=string::npos=4294967295,查詢不包含子串中的任何字元,返回第一個位置。從當前串中查詢第一個不在引數串中的字元出現的位置
n=s.find_first_not_of(3); //n=0
n=s.find_first_not_of('dfg'); //n=0
n = s.find_first_not_of("dfg");//n=0
n=s.find_first_not_of("dhh"); //n=0
n = s.find_first_not_of("wer");//n=0
n=s.find_first_not_of('f'); //n=0
n = s.find_first_not_of("f"); //n=0
n = s.find_last_not_of("asdfg");//n=string::npos=4294967295,查詢不包含子串中的任何字元,返回最後一個位置
n=s.find_last_not_of(3); //n=4
n=s.find_last_not_of('dfg'); //n=3
n = s.find_last_not_of("dfg"); //n=1
n=s.find_last_not_of("dhh"); //n=4
n = s.find_last_not_of("wer"); //n=4
n=s.find_last_not_of('f'); //n=4
n = s.find_last_not_of("f"); //n=4
注:找到就返回第一次出現的串的第一個字元位置,找不到返回-1。
1.find()用法(查詢指定字串是否存在)string中find()返回值是字母在母串中的位置(下標記錄),如果沒有找到,那麼會返回一個特別的標記npos。(返回值可以看成是一個int型的數)
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
////find函式返回型別 size_type
string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//find 函式 返回jk 在s 中的下標位置
position = s.find("jk");
if (position != s.npos) //如果沒找到,返回一個特別的標誌c++中用npos表示,我這裡npos取值是4294967295,
{
printf("position is : %d\n" ,position);
}
else
{
printf("Not found the flag\n");
}
}
2.查詢某一給定位置後的子串的位置
//從字串s 下標5開始,查詢字串b ,返回b 在s 中的下標
position=s.find("b",5);
- 查詢所有子串在母串中出現的位置
//查詢s 中flag 出現的所有位置。
flag="a";
position=0;
int i=1;
while((position=s.find(flag,position))!=string::npos)
{
cout<<"position "<<i<<" : "<<position<<endl;
- Rfind()用法,反向查詢子串在母串中出現的位置,通常我們可以這樣來使用,當正向查詢與反向查詢得到的位置不相同說明子串不唯一。
flag="jkll";
position=s.rfind (flag);
printf("s.rfind (flag) :%d\n",position);
反向查詢所有的
flag="jk";
position=s.length()-1;
int i=1;
while((position=s.rfind(flag,position))!=string::npos)
{
cout<<"position "<<i<<" : "<<position<<endl;
position--;
i--;
}
String中的插入
#include <iostream>
#include <string>
#include<cstring>
using namespace std;
int main (){
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); //加一個'.' to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )*/
cout << str << '\n';
return 0;
}
String中的刪除
- string.erase(pos,n) //刪除從pos開始的n個字元 string.erase(0,1); 刪除第一個字元
string s;
s.erase(1,2);
- string.erase(pos) //刪除pos處的一個字元(pos是string型別的迭代器)
i = s.begin()+3;
s.erase(i);
3、string.erase(first,last) //刪除從first到last中間的字元(first和last都是string型別的迭代器)
s.erase(s.begin()+1,s.end()-1);
相關推薦
最實用的的c++中string函式的用法,沒有之一。
純屬原創, String函式的用法
C#中string.format用法詳解
個數 date 其中 位置 tr1 bsp 位數 數值 日期格式化 tring.Format 方法的幾種定義: String.Format (String, Object) 將指定的 String 中的格式項替換為指定的 Object 實例的值的文本等效項。String.F
C++中sort函式用法
C++中sort函式用法 排序示例: 輸入兩個數n,t,其中n是待排的結構體個數,t=0代表用降序排序,t = 1表示用升序排序 例如這樣: 例示: jack 70 peter 96 Tom 70 smith 67 從高到低 成績 peter 96 jack 70
C++中string的用法
gin 替換 手寫 是否 如何 定義 ins app 比較 我們知道string可以構造一個字符串變量,那麽它的操作有哪些呢。它包含在string庫中(不是string.h沒有.h),它可以和定義一個字符一樣定義一個字符串變量,而且強大的C++還內置了各種函數,基本實現不用
c++中replace函式用法總結
一、用法一 string& replace (size_t pos, size_t len, const string& str) 用str 替換指定字串從起始位置pos開始長度為le
C++中substr()函式用法
1234 assign()函式: basic_string &assign( const basic_string &str ); basic_string &assign( const char *str ); basic_string &assign( const c
C++中find函式用法
C++中STL裡提供了許多字串操作的函式,下面是字串查詢方面的部分函式用法簡介: 1.find() 查詢第一次出現的目標字串: #include<iostream> #include<
C++中sort()函式用法
做專案的時候,排序是一種經常要用到的操作。如果每次都自己寫個冒泡之類的O(n^2)排序,不但程式容易超時,而且浪費寶貴的時間,還很有可能寫錯。STL裡面有個sort函式,可以直接對陣列排序,複雜度為n*log2(n)。 sort是STL中提供的演算法,標頭檔案為#inclu
C 中scanf ( ) 函式用法 用法
我覺得,在輸入輸出函式中,scanf()函式,應該是最麻煩的,有時它給我們的結果很可笑,但是一定是一原因的.... 首先宣告一下,這篇日誌不是介紹scanf()中各種格式符用法的文章(沒有這個必要,但是大家一定要會用). 我嘗試了很多種輸入,包括一些錯誤的練習,曾經對
C++中string的用法和例子(1) 插入 擷取子字串 刪除
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); string str="to be question"; string str2="the ";
為什麼大家都覺得C++是最難的程式語言,沒有之一。
很多已經做了幾年的C++程式設計師已經很自信覺得這門程式語言算是熟悉階段了,但是當重新對這門語言來個徹底大掃除的時候發現,又有新的語法出現,最糟糕的是之前掌握的很多套路未必是對的。以至於很多程式設計師段子手留下了很多精彩的話語 你會發現等你把C++用熟練了,再去學習別的語言,那感覺爽呆了
Excel 中 percentileRank 函式用法人工窮舉。
以下引自office 助手: 而在 Excel 2010 中新提供兩個函式 PERCENTRANK.INC 與 PERCENTRANK.EXC 可以達成這個目的。這兩個函式都可以把學生的個別成績轉換成佔全體成績的百分比,差別只在於 PERCENTRANK.INC 把唯一最
新手必看,史上最全的iOS開發教程集錦,沒有之一!
最近大火的iPhone XS Max和iPhone XS,不知道有沒有同學已經下手了呢?一萬三的價位確實讓很多人望而卻步啊。據說為了贏得中國的使用者,專門出了雙卡雙待的,可想而知中國市場這塊“肥肉”人人都想要。 近幾年,無論蘋果出什麼樣的產品以及多高的價位,都會有非常多的蘋
最簡單的Go Dockerfile編寫姿勢,沒有之一!
## 1. Dockerfile一些額外注意點 * 選擇最簡單的映象 比如alpine,整個映象5M左右 * 設定映象時區 ```dockerfile RUN apk add --no-cache tzdata ENV TZ Asia/Shanghai ``` ## 2. 多階段
nginx 基本入門(至今為止見過最好的 nginx 入門文章,沒有之一。)
這篇教程簡單介紹了 nginx 並且講解了一些 nginx 可以解決的簡單任務。這裡,我們假設 nginx 已經安裝在讀者的機器上。如果沒有,可以看一下如何安裝 nginx。這篇教程主要講解的是如果啟用和停止 ngixn,和重新載入配置,描述配置檔案的基本結構和怎樣搭建一個 nginx 靜態輔助器,
C++中string常用函式及用法總結
標準c++中string類函式介紹 注意不是CString 之所以拋棄char*的字串而選用C++標準程式庫中的string類,是因為他和前者比較起來,不必 擔心記憶體是否足夠、字串長度等等,而且作為一個類出現,他整合的操作函式足以完成我們大多數情況下(甚至是1
C++中Main函式引數argc,argv用法
int main(int argc, char** argv)argc為傳入引數個數,argv是具體的引數。我知道的兩種使用方法:1.在命令列中使用,如傳入"test.exe f:\a.txt f:\b.txt",那麼:argc=3;argv[0] = test.exe (
C++中仿函式/函式物件,函式指標的用法
研究這個起因是這樣的,就是今天在用priority_queue的時候,需要自定義比較函式,但是此時又不能修改需要比較的類的內容(即不能用過載<的方法),所以只能寫在外面,但是發現這樣並不能編譯通過。報的錯叫cmp(我寫的那個比較函式)不是型別名。後來
C++ 中string.find() 函式的用法總結
#include <string> #include <iostream> using namespace std; void main() { ////find函式返回型別 size_typestring s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8
C++ 中string.find() 函式的用法總結(轉載)
#include <string>#include <iostream>using namespace std; void main() { ////find函式返回型別 size_type string s("1a2b3c4d5e6f7g8