NX二次开发-以指定字符分割字符串
1 std::vector<std::string> SplitString(std::string str, const std::string& seperator) 2 { 3 std::vector<std::string> result; 4 int seperatorSize = seperator.size(); 5 TrimString(str); 6 if (str.empty()) 7 { 8 return result; 9 } 10 std::size_t pos = str.find(seperator); 11 while (pos != std::string::npos) 12 { 13 std::string tempStr = str.substr(0, pos); 14 TrimString(tempStr); 15 result.push_back(tempStr); 16 str = str.substr(pos + seperatorSize); 17 TrimString(str); 18 pos = str.find(seperator); 19 } 20 result.push_back(str); 21 return result; 22 }
1 void TrimString(std::string& result, char trimChar) 2 { 3 if (result.empty()) 4 { 5 return; 6 } 7 8 std::size_t pos = result.find_last_not_of(trimChar); 9 if (pos != std::string::npos) 10 { 11 result.erase(pos + 1); 12 pos = result.find_first_not_of(trimChar); 13 result.erase(0, pos); 14 } 15 else 16 { 17 result.erase(result.begin(), result.end()); 18 } 19 }