を行う最も簡単な方法であると思われ、私は、文字列を操作するための静的メソッドの束を持っているユーティリティクラスを持っています。文字列を分割したり、区切り文字の部分文字列を使って文字列をトークン化する方法だけでなく、空白のトリミングに関連する関数を示します。
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;
}
今、あなたはあなたのニーズに合ったこれらのメソッドを修正することができるが、これは、私は、ライブラリを操作する再利用可能な文字列を使う一般的な考え方です。
文脈による文字列置換の場合、['^ \ s + | \ s + $ | \ s *(:)\ s *' - > '$ 1'](https://)のような正規表現を使用したいと思います。 regex101.com/r/J1wguG/1)。 –
あなたは何をしようとしているのか、あなたは私たちにプログラムを見せてくれました。プログラムはあなたが何を記述していないのですか?そうでない場合、それは何をしますか?何が問題ですか? – user2079303
私はそれを実行すると、ファイルから読み取った最初の行にエラーが表示されます。 'std :: out_of_range'のインスタンスをスローした後に終了するようにしました what():basic_string :: at:__n )> = this-> size()(これは20です) – pasi68