поделить текст на пробелы и записать в таблицу

DZONE

Известный
Автор темы
187
199
Версия MoonLoader
Другое
пример:

Lua:
"привет мир привет я"

вывод:
local table = {
    "привет",
    "мир",
    "привет",
    "я"
}
 
Решение
пример:

Lua:
"привет мир привет я"

вывод:
local table = {
    "привет",
    "мир",
    "привет",
    "я"
}
Lua:
function split(source, sep)
    local result, i = {}, 1
    while true do
        local a, b = source:find(sep)
        if not a then break end
        local candidat = source:sub(1, a - 1)
        if candidat ~= "" then
            result[i] = candidat
        end i=i+1
        source = source:sub(b + 1)
    end
    if source ~= "" then
        result[i] = source
    end
    return result
end

local destination_number="ya fanat chapo"

local result = split(destination_number, "[%s]+")
for i, v in ipairs(result) do
    print(v)
end
88839FD9-C640-41D2-B650-38F497BF407F.png

Дальше разберешься?

UPD: там вроде можно как-то через string.match...

YarikVL

Известный
Проверенный
4,738
1,816
пример:

Lua:
"привет мир привет я"

вывод:
local table = {
    "привет",
    "мир",
    "привет",
    "я"
}
Lua:
function split(source, sep)
    local result, i = {}, 1
    while true do
        local a, b = source:find(sep)
        if not a then break end
        local candidat = source:sub(1, a - 1)
        if candidat ~= "" then
            result[i] = candidat
        end i=i+1
        source = source:sub(b + 1)
    end
    if source ~= "" then
        result[i] = source
    end
    return result
end

local destination_number="ya fanat chapo"

local result = split(destination_number, "[%s]+")
for i, v in ipairs(result) do
    print(v)
end
88839FD9-C640-41D2-B650-38F497BF407F.png

Дальше разберешься?

UPD: там вроде можно как-то через string.match и цикл for но мне лень проверять новый метод разделения)
 
  • Эм
  • Вау
  • Нравится
Реакции: whyega52, DZONE и ARMOR