find_first_of()和 find_last_of() 【獲取路徑、文件名】

原文轉載自:http://blog.sina.com.cn/s/blog_68208a890100v608.html    

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.
              該搜索在字符末尾查找匹配,所以沒有提供起始位置。
     
     示例:
   

  1. <span style="font-family:Courier New;font-size:16px;">     string str = "Mississippi";  
  2.      int index;  
  3.      // 's ' 在index 爲 2、3、5、6處出現  
  4.      index = str.find_first_of('s',0);    // index爲 2  
  5.      index = str.find_first_of('s',4);    // index爲 5  
  6.      index = str.find_first_of('s',7);    // index爲 -1  
  7.       
  8.      // ‘s’的最後出現在 index= 6  
  9.      index = str.find_last_of('s');  
  10.      // while 循環輸出每個'i'的index  
  11.      while((index = str.find_first_of('i', index))!= -1)  
  12.      {  
  13.         cout << "index" << index << " ";  
  14.         index++;   // restart search at next indx  
  15.      }</span>  
  16.        

   輸出結果: 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,函數搜索整個字符串。
       
    示例  
  

  1. <span style="font-family:Courier New;font-size:16px;">    string fullname = "Mark Tompkin", firstname, lastname;  
  2.     int index;  
  3.      
  4.     index = str.find_last_of(' ');   // index is 4  
  5.     // firstname = "Mark" lastname = "Tompkin"  
  6.     firstname = fullname.sub string(0,index);  
  7.     lastname = fullname.substring(index+1);  
  8.      
  9.     index = fullname.find("kin");         // 在 index = 9 匹配 "Kin"  
  10.     index = fullname.find("omp",0);    // 在 index = 6 匹配 "omp"  
  11.     index = fullname.find("omp",7);    // index is -1 (無匹配)</span>  
   

    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的空字符串。
     
     示例:
   

  1. <span style="font-family:Courier New;font-size:16px;">     string str = "endfile";  
  2.      string s = "string object type";  
  3.      str += " mark";  
  4.      str.inset(3,   "-of-"); // str 是 "end-of-file mark"  
  5.      s.erase(7,7);        // s 是 "string type"  
  6.      // 從index 爲3處刪除4個字符  
  7.      s.erase(3,4);  
  8.      cout << s;          // 輸出:"strtype"</span>  
     

    4.c_str()返回c語言風格字符串的地址。

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

  1. <span style="font-family:Courier New;font-size:16px;">string filename = "input.dat";  
  2.  // open 要求文件名是c語言風格的字符串  
  3. fin.open(filename.c_str());</span>  

   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"
     

  1. <span style="font-family:Courier New;">// WJ.cpp : 定義控制檯應用程序的入口點。  
  2. //  
  3. #i nclude "stdafx.h"  
  4. #i nclude<iostream>  
  5. #i nclude<string>  
  6.   
  7. using namespace std;  
  8.   
  9. int main()  
  10. {  
  11. string pathname, path, filename,executableFile;  
  12. // ‘/’和 '.'的位置  
  13. int backslashIndex, dotIndex;  
  14. cout << "Enter the path name: ";  
  15. cin >> pathname;  
  16.   
  17. // 識別最後一個'/'的位置。注意:由於  
  18. // 轉義碼如'/n'以/起始,  
  19. // c++ 使用'//'表示 /  
  20.   
  21. backslashIndex = pathname.find_last_of('//');  
  22.   
  23. //路徑名是最後一個'/'之前的字符  
  24. path = pathname.substr(0,backslashIndex);  
  25.   
  26. cout << "path:     " << path << endl;  
  27.   
  28. // 路徑名尾部是文件名  
  29. filename = pathname.substr(backslashIndex+1,-1);  
  30. cout << "Filename: " << filename << endl;  
  31.   
  32. // 查看文件名是否有'.cpp'擴展名。  
  33. // 首先找到最後一個'.'的位置。 如果  
  34. // 沒有'.',則dotIndex爲-1  
  35. dotIndex = filename.find_last_of('.');  
  36. //測試是否有'.',其餘的字符是否爲"cpp"  
  37. if (dotIndex != -1 && filename.substr(dotIndex+1) == "cpp")  
  38. {  
  39.    // 刪除"cpp",並加上"exe"設置可執行字符串  
  40.    executableFile = filename;  
  41.    executableFile.erase(dotIndex+1,3);  
  42.    executableFile+="exe";  
  43.    cout << "Executable: " << executableFile << endl;  
  44. }  
  45.   
  46. return 0;  
  47. }      </span>  

   輸出結果:
   第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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章