2016-07-31 70 views
1

我正在將lua移植到chaiscript。原來的Lua代碼使用splitchaiscript字符串可以像lua一樣分裂嗎?

function string:split(delimiter) 
    local result = { } 
    local from = 1 
    local delim_from, delim_to = string.find(self, delimiter, from ) 
    while delim_from do 
     table.insert(result, string.sub(self, from , delim_from-1)) 
     from = delim_to + 1 
     delim_from, delim_to = string.find(self, delimiter, from ) 
    end 
    table.insert(result, string.sub(self, from )) 
    return result 
end 

但我找不到split chaiscript的版本...是否chaiscript有split功能?

回答

2

ChaiScript的string功能是C++,std::string中可用內容的直接映射。所以,沒有沒有內置的拆分功能。

0

您可以輕鬆地使用自己的一個像一個在這裏找到:Split a string in C++?

#include <string> 
#include <sstream> 
#include <vector> 

void split(const std::string &s, char delim, std::vector<std::string> &elems) { 
    std::stringstream ss; 
    ss.str(s); 
    std::string item; 
    while (std::getline(ss, item, delim)) { 
     elems.push_back(item); 
    } 
} 


std::vector<std::string> split(const std::string &s, char delim) { 
    std::vector<std::string> elems; 
    split(s, delim, elems); 
    return elems; 
}