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

來源:互聯網
上載者:User

標籤:名稱   pen   使用字串   enter   定位   簡單   複製檔案   int start   tom   

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的Null 字元串。
    
     樣本:
     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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.