2016-12-15 11 views
-2

私の問題は、文字列から空白を削除する必要がありますが、文字列の特定の場所だけです。 .txtファイルを1行ずつ読み込み、ファイル内の行を次のようにするプログラムがあります。文字列の特定の箇所から空白を削除する

|sometext:anothertext| 

|some text : anothertext| 

|some text:another text | 

| sometext : anothertext | 

'|'行の開始と終了をマークしています。

ここでは、行の前後にある空白を削除するだけで、 ':'の隣には空白を削除する必要があります。

編集:正規表現は、この

+0

文脈による文字列置換の場合、['^ \ s + | \ s + $ | \ s *(:)\ s *' - > '$ 1'](https://)のような正規表現を使用したいと思います。 regex101.com/r/J1wguG/1)。 –

+0

あなたは何をしようとしているのか、あなたは私たちにプログラムを見せてくれました。プログラムはあなたが何を記述していないのですか?そうでない場合、それは何をしますか?何が問題ですか? – user2079303

+0

私はそれを実行すると、ファイルから読み取った最初の行にエラーが表示されます。 'std :: out_of_range'のインスタンスをスローした後に終了するようにしました what():basic_string :: at:__n )> = this-> size()(これは20です) – pasi68

答えて

0

を行う最も簡単な方法であると思われ、私は、文字列を操作するための静的メソッドの束を持っているユーティリティクラスを持っています。文字列を分割したり、区切り文字の部分文字列を使って文字列をトークン化する方法だけでなく、空白のトリミングに関連する関数を示します。

Utility.h

#ifndef UTILITY_H 
#define UTILITY_H 

class Utility { 
public: 
    static std::string trim(const std::string& str, const std::string elementsToTrim = " \t\n\r"); 
    static std::vector<std::string> splitString(const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty = true); 

private: 
    Utility(); // Private - Not A Class Object 
    Utility(const Utility& c); // Not Implemented 
    Utility& operator=(const Utility& c); // Not Implemented 

}; // Utility 

#endif // UTILITY_H 

Utility.cpp

#include "stdafx.h" 
#include "Utility.h"  

// ---------------------------------------------------------------------------- 
// trim() 
// Removes Elements To Trim From Left And Right Side Of The str 
std::string Utility::trim(const std::string& str, const std::string elementsToTrim) { 
    std::basic_string<char>::size_type firstIndex = str.find_first_not_of(elementsToTrim); 
    if (firstIndex == std::string::npos) { 
     return std::string(); // Nothing Left 
    } 

    std::basic_string<char>::size_type lastIndex = str.find_last_not_of(elementsToTrim); 
    return str.substr(firstIndex, lastIndex - firstIndex + 1); 
} // trim  

// ---------------------------------------------------------------------------- 
// splitString() 
std::vector<std::string> Utility::splitString(const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty) { 
    std::vector<std::string> vResult; 
    if (strDelimiter.empty()) { 
     vResult.push_back(strStringToSplit); 
     return vResult; 
    } 

    std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd; 
    while (true) { 
     itSubStrEnd = search(itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end()); 
     std::string strTemp(itSubStrStart, itSubStrEnd); 
     if (keepEmpty || !strTemp.empty()) { 
      vResult.push_back(strTemp); 
     } 

     if (itSubStrEnd == strStringToSplit.end()) { 
      break; 
     } 

     itSubStrStart = itSubStrEnd + strDelimiter.size(); 
    } 

    return vResult;  
} // splitString 

これらのメソッドは、単にヘッダーと必要な使用スコープ解決演算子以降を含めるあなたを助ける必要があり、それらを使用しますこれらのメソッドは静的で、「ユーティリティオブジェクト」をインスタンス化または定義することはできません。

#include "stdafx.h" 
#include "Utility.h" 

int main() { 
    std::string myString("TwoWords"); 
    std::string delimiter("Two"); 
    std::vector<std::string> words; 

    words = Utility::splitString(myString, delimiter); 
    // Or 
    words = Utility::splitString(myString, std::string(" ")); 
    // Or 
    words = Utility:splitString(myString, " "); 

    std::string sentence(" Hello World! "); 

    sentence = Utility::trim(sentence); // 2nd Parameter Default To Whitespace 

    return 0; 
} 

今、あなたはあなたのニーズに合ったこれらのメソッドを修正することができるが、これは、私は、ライブラリを操作する再利用可能な文字列を使う一般的な考え方です。

関連する問題