close

感謝

Mr. Opengate.  http://mropengate.blogspot.com/2015/07/cc-string-stl.html

BYvoid 和 Nicolai M.Josuttis  https://www.byvoid.com/zht/blog/cpp-string

bcr   https://stackoverflow.com/q/5590381

string 介紹

string是保存字符的序列容器,簡單來說就是存文字用的,使用前須使用#include <string>

string用法

賦值 = :將字串指定給另一個字串。

比較 ==:比較兩個字串是否相等。

串接 +:串接兩字串

.erase(first,num) :清空字串從first開始的num個字符。

.substr(first,num) :取得從first開始的num個字符。

.clean() :清空整的字串。

.length() :取得字串的長度。

.size() :取得字串的長度。

.assign(string2,first,length) :將string2中從first開始的length個字指定給此string。

.append(string2,first,length) :將string2中從first開始的length個字接到此string後面。

.find("example",place) :從place的位置開始尋找第1個example。

.find_first_of(string2) :在此字串中,從第一個字符開始,尋找string2中的任何字符。

.find_first_not_of(string2) :在此字串中,從第一個字符開始,尋找不包括string2中的任何字符。

.find_last_of(string2) :在此字串中,從最後一個字符開始,尋找string2中的任何字符。

.find_last_not_of(string2) :在此字串中,從最後一個字符開始,尋找不包括string2中的任何字符。

 

#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str1,str2,str3,str4;
    cout<<"輸入一個字串 , ";
    cin>>str1;
	//1.輸出str1
    cout<<"1.str1 = "<<str1<<endl;
    //2.清空字串指定位置 
    str1.erase(0,1);
    cout<<"2.str1 = "<<str1<<endl;
    //3.字串賦值 
    str2=str1;
    cout<<"3.str2 = "<<str2<<endl;
    //4.string的串接 
    str1=str1+" behind";
    cout<<"4.str1 = "<<str1<<endl;
    //5.string串接 
    str1="front "+str1;
    cout<<"5.str1 = "<<str1<<endl; 
    //6.兩個取得string的長度 
    cout<<"6.str1的長度 = "<<str1.length()<<endl;
    cout<<"6.str1的長度 = "<<str1.size()<<endl;
    //7.存取str1的字 
    cout<<"7.str1第一個字 = "<<str1.at(0)<<endl;
    cout<<"7.str1第二個字 = "<<str1[1]<<endl;

輸出結果

輸入一個字串 , ihello
1.str1 = ihello
2.str1 = hello
3.str2 = hello
4.str1 = hello behind
5.str1 = front hello behind
6.str1的長度 = 18
6.str1的長度 = 18
7.str1第一個字 = f
7.str1第二個字 = r

	//8.清空str3 
    str3.clear();
    cout<<"8.str3 = "<<str3<<endl;
    //9.兩字串的串接 
    str3=str3+str1;
    //比較兩字串 
    if(str3==str1)
    {
        cout<<"9.str1 = str3 = "<<str2<<endl;
    }
    //10.指定字串 
    str4.assign(str1,6,5);
    cout<<"10.str1 = "<<str1<<endl;
    cout<<"10.str4 = "<<str4<<endl;
    //11.指定字串加到另一字串 
    str4.append(str1,6,5);
    cout<<"11.str4 = "<<str4<<endl;
	//12.插入字串 
    str4.insert(5," add ");
    cout<<"12.將add加入str4 = "<<str4<<endl;
    str4.insert(5,str2);
    cout<<"12.將str2加入str4 = "<<str4<<endl;
    //13.尋找字串 
    cout<<"13.在str4尋找add在第幾個位置 = "<<str4.find("add",0)<<endl;
    //14.尋找不到字串
    cout<<"14.str3 = "<<str3<<endl;
    cout<<"14.在str3尋找add在第幾個位置 = "<<str3.find("add",0)<<endl;
    //15.我真的不知道 string::npos是甚麼 求大大指點 
    cout<<"15.string::npos = "<<string::npos<<endl;
    return 0;   
}

輸出結果

8.str3 =
9.str1 = str3 = hello
10.str1 = front hello behind
10.str4 = hello
11.str4 = hellohello
12.將add加入str4 = hello add hello
12.將str2加入str4 = hellohello add hello
13.在str4尋找add在第幾個位置 = 11
14.str3 = front hello behind
14.在str3尋找add在第幾個位置 = 18446744073709551615
15.string::npos = 18446744073709551615

int 轉 string

使用前,前面請加上#include <sstream> 

#include <iostream>
#include <string>
#include <sstream> 
using namespace std;
int main ()
{
	int a;
	cout<<"輸入一int,";
	cin>>a;
	stringstream ss;
	ss << a;
	string str = ss.str();
	cout<<"str = "<<str;
	return 0;	
}

輸出結果

輸入一int,1511
str = 1511

string應用

過濾一行開頭和結尾的所有非英文字符

#include <string>
#include <iostream>
using namespace std;
int main()
{
	string str1="///...**/work smart--/*/-**+///";
	string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	int first = str1.find_first_of(strset);//first為找到的位置 資料型態是int
	string::size_type stf=str1.find_first_of(strset);//功能與first相同但資料型態不同
	if(stf==string::npos )
	{
		cout<<"not find any characters"<<endl;
		return -1;
	}
	string::size_type stl= str1.find_last_of(strset);
	if(stl==string::npos )
	{
		cout<<"not find any characters"<<endl;
		return -1;
	}
	//得到字串的指定位置
	cout<<"str1 = "<<str1.substr(stf,stl-stf+1)<<endl;

輸出結果

str1 = work smart

	//試用 find_first_not_of  find_last_not_of
    string str2="work /*-*/ smart";
    string::size_type stf2=str2.find_first_not_of(strset);
    if(stf2==string::npos )
    {
        cout<<"not find any characters"<<endl;
        return -1;
    }
    string::size_type stl2= str2.find_last_not_of(strset);
    if(stl==string::npos )
    {
        cout<<"not find any characters"<<endl;
        return -1;
    }
    //清空字串指定位置 
    cout<<"str2 = "<<str2.erase(stf2,stl2-stf2)<<endl;
    return 0;
}

輸出結果

str2 = work smart

心得

經過這幾天的解題,我發現string很常使用,幾乎都是int和string就可以了。

所以,我想先把string研究一下。所以把一些用法都用過一次,在此分享給大家,希望可以幫助到其他人。

如果有錯誤請馬上告知。

感謝在網路上分享經驗的大大。

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 豪CO 的頭像
    豪CO

    程式道路,必為豐富

    豪CO 發表在 痞客邦 留言(0) 人氣()