c++中字符串的截取:

c++中字符串的截取:ios

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

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.  
    string str = "Mississippi";
  2.  
    int index;
  3.  
    // 's ' 在index 爲 二、三、五、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.  
    }


   輸出結果: 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.  
    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 (無匹配)

   

 

    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.  
    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"

     

 

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

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

  1.  
    string filename = "input.dat";
  2.  
    // open 要求文件名是c語言風格的字符串
  3.  
    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"
     spa

  1.  
    // 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.  
    }


   輸出結果:
   第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.net

 

 

 

 

from:https://blog.csdn.net/zhenyusoso/article/details/7286456rest

相關文章
相關標籤/搜索