NX二次开发-以指定字符分割字符串

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 SplitStringToList(std::string inputStr, std::string seperator, std::vector<std::string> &outputList)
 2 {
 3     outputList.clear();
 4 
 5     TrimString(inputStr);
 6     std::size_t pos = inputStr.find(seperator);
 7 
 8     while (pos != std::string::npos)
 9     {
10         std::string item = inputStr.substr(0, pos);
11         TrimString(item);
12         outputList.push_back(item);
13         inputStr = inputStr.substr(pos + 1);
14         pos = inputStr.find(seperator);
15     }
16 
17     TrimString(inputStr);
18     if (!inputStr.empty())
19     {
20         outputList.push_back(inputStr);
21     }
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     }
原文地址:https://www.cnblogs.com/xiang-L/p/14132779.html