1. 程式人生 > >C++中路徑的處理方法(string)

C++中路徑的處理方法(string)

ble fin ase names 復制字符串 ostream 是否 substr() 新的

string 類提供字符串處理函數,利用這些函數,程序員可以在字符串內查找字符,提取連續字符序列(稱為子串),以及在字符串中刪除和添加。我們將介紹一些主要函數。

1.函數find_first_of()和 find_last_of() 執行簡單的模式匹配,如在字符串中查找單個字符c。函數find_first_of() 查找在字符串中第1個出現的字符c,而函數find_last_of()查找最後一個出現的c。匹配的位置是返回值。如果沒有匹配發生,則函數返回-1.

int find_first_of(char c, int start = 0):
查找字符串中第1個出現的c,由位置start開始。 如果有匹配,則返回匹配位置;否則,返回-1.默認情況下,start為0,函數搜索整個字符串。

int find_last_of(char c):
查找字符串中最後一個出現的c。有匹配,則返回匹配位置;否則返回-1.該搜索在字符末尾查找匹配,所以沒有提供起始位置。

示例:
string str = "Mississippi";
int index;
// ‘s ‘ 在index 為 2、3、5、6處出現
index = str.find_first_of(‘s‘,0); // index為 2
index = str.find_first_of(‘s‘,4); // index為 5
index = str.find_first_of(‘s‘,7); // index為 -1

// ‘s’的最後出現在 index= 6
index = str.find_last_of(‘s‘);
// while 循環輸出每個‘i‘的index
while((index = str.find_first_of(‘i‘, index))!= -1)
{
cout << "index" << index << " ";
index++; // restart search at next indx
}

輸出結果: index 1 index 4 index 7 index 10

2.字符串中提取連續字符序列,既子串。
這個操作假定位置 start 和 字符數 count.

string substr(int start=0,int count= -1);
從起始位置開始復制字符串中的count 個字符,並返回這些字符作為子串。
如果字符串尾部小於count字符或者count 為-1,則字符串尾停止復制。
如果不使用參數調用只包括位置start,則substr()返回從位置開始到字符串尾部的子串。

find()函數在字符串中查找指定模式。該函數將字符串s和位置start作為參數,並查找s的匹配作為子串。

int find(const string& s,int start = 0):
該搜索獲得字符串s和位置start,並查找s的匹配作為子串。如果有匹配,則返回匹配的位置;否則返回-1。 默認情況下,start為0,函數搜索整個字符串。

示例:
string fullname = "Mark Tompkin", firstname, lastname;
int index;

index = str.find_last_of(‘ ‘); // index is 4
// firstname = "Mark" lastname = "Tompkin"
firstname = fullname.sub string(0,index);
lastname = fullname.substring(index+1);

index = fullname.find("kin"); // 在 index = 9 匹配 "Kin"
index = fullname.find("omp",0); // 在 index = 6 匹配 "omp"
index = fullname.find("omp",7); // index is -1 (無匹配)

3.添加和刪除字符串

字符連接(+、+=)是在字符串尾添加字符串。insert()函數擴展了這個能力,允許在任意位置添加字符串。為了從字符串。為了從字符串中刪除字符串,
函數erase()可以從指定的位置開始刪除字符。

void insert(int statr,const string& s):
將子串s放入字符串中,起始於位置start。插入操作增加了原始字符串的長度。

void erase(int start=0,int count=-1):
start開始,從字符串中刪除count個字符。如果現有的字符串少於count個字符,或者count為-1,則刪除到字符串尾部的所有字符。默認情況下,start為0,函數從字符串是起始位置開始刪除字符串。默認情況下,函數也刪除到字符串尾。需要註意的是,不使用參數調用erase()函數時,將把字符串截斷為長度為0的空字符串。

示例:
string str = "endfile";
string s = "string object type";
str += " mark";
str.inset(3, "-of-"); // str 是 "end-of-file mark"
s.erase(7,7); // s 是 "string type"
// 從index 為3處刪除4個字符
s.erase(3,4);
cout << s; // 輸出:"strtype"

4.c_str()返回c語言風格字符串的地址。
將字符串對象轉換為c語言風格字符串。
char *c_str();
返回一個等價於字符串對象的c語言風格字符串的地址。返回類型char*表示c語言風格字符串第1個字符的地址。

示例:
string filename = "input.dat";
// open 要求文件名是c語言風格的字符串
fin.open(filename.c_str());

5.分離字符串路徑的方法

處理文件的程序可能要分析文件名。這種算法要進行字符串處理。文件可以由路徑名指定,路徑名包括由分隔符"\"分割的名稱集。最後一個"\"前的名稱序列稱為路徑。最後一個名稱是文件名,還可能包括擴展名。

路徑名 \class\programs\testfile.cpp
路徑 \class\programs\
文件名 testfile.cpp
擴展名 cpp

為了分析文件名,我們從鍵盤讀入完整的路徑名,並輸出路徑和文件名。如果文件名具有擴展名"cpp",則在創建可執行文件名時,將用"exe"替代擴展名"cpp".下面是程序結構的輪廓,以及如何使用字符串函數的說明:

1.輸入文件名,使用函數find_last_of()在字符串中搜索最後一個出現的"\"。這個字符確定了路徑的結尾和文件名的開始。
2。路徑是由最後一個"\"前所有字符串組成的子串。文件名是最後一個"\"後的所有字符。使用最後一個"\"的位置和substr()提取出路徑和文件名。
3.擴展名是文件名中最好一個"."後的字符串。調用find_last_of()搜索最後一個匹配,則復制文件名,刪除當前擴展名,並添加新的擴展名"exe"。 輸出產生的可執行文件名。

// 文件prg1_3.cpp
// 此程序提示用戶輸入文件的路徑
// 它使用string類操作來識別並輸出
// 路徑名和文件名。如果文件名有
// 擴展名"cpp",則創建並輸出
// 可執行文件的名稱,其擴展名為"exe",替換
// 擴展名"cpp"

// WJ.cpp : 定義控制臺應用程序的入口點。
//
i nclude "stdafx.h"
i nclude<iostream>
i nclude<string>

using namespace std;

int main()
{
string pathname, path, filename,executableFile;
// ‘\’和 ‘.‘的位置
int backslashIndex, dotIndex;
cout << "Enter the path name: ";
cin >> pathname;

// 識別最後一個‘\‘的位置。註意:由於
// 轉義碼如‘\n‘以\起始,
// c++ 使用‘\\‘表示 \

backslashIndex = pathname.find_last_of(‘\\‘);

//路徑名是最後一個‘\‘之前的字符
path = pathname.substr(0,backslashIndex);

cout << "path: " << path << endl;

// 路徑名尾部是文件名
filename = pathname.substr(backslashIndex+1,-1);
cout << "Filename: " << filename << endl;

// 查看文件名是否有‘.cpp‘擴展名。
// 首先找到最後一個‘.‘的位置。 如果
// 沒有‘.‘,則dotIndex為-1
dotIndex = filename.find_last_of(‘.‘);
//測試是否有‘.‘,其余的字符是否為"cpp"
if (dotIndex != -1 && filename.substr(dotIndex+1) == "cpp")
{
// 刪除"cpp",並加上"exe"設置可執行字符串
executableFile = filename;
executableFile.erase(dotIndex+1,3);
executableFile+="exe";
cout << "Executable: " << executableFile << endl;
}

return 0;
}
輸出結果:
1次允許結果:

Enter the path name: \class\programs\testfile
path: \class\programs
Filename: testfile

2次允許結果:

Enter the path name: programs\strings\filedemp.cpp
path: programs\strings
Filename: filedemo.cpp
Executable: filedemo.exe

3次允許結果:

Enter the path name: \program.cpp
path:
Filename: program.cpp
Executable: program.exe

C++中路徑的處理方法(string)