2013-10-19 63 views
2

字符串我不知道SQLite的,但我要實現已經完成的數據庫。我正在使用Corona SDK進行編程。問題:我有一個名爲「答案」的列,格式如下:House,40|Bed,20|Mirror,10 ecc。 我想拆分字符串,並刪除"," "|"這樣的:拆分對sqlite的

VARIABLE A=House 

VARIABLE A1=40 


VARIABLE B=Bed 

VARIABLE B1=20 

VARIABLE C=Mirror 

VARIABLE C1=10 

我爲我的英語很抱歉。感謝大家。

+0

你可以獲取每個行的值,並在Java中使用拆分功能拆分它。 –

+1

Corona SDK基於Lua語言 – Nick21

+0

噢,謝謝你的信息。我沒有意識到這一點,我是在電暈sdk可以成爲Android的一部分的概念,所以它支持java :) ..謝謝.. –

回答

1

試試這個:

如果你想簡單地刪除字符,那麼你可以使用以下命令:

更新3:

local myString = "House;home;flat,40|Bed;bunk,20|Mirror,10" 
local myTable = {} 
local tempTable = {} 

local count_1 = 0 
for word in string.gmatch(myString, "([^,|]+)") do 
    myTable[#myTable+1]=word 
    count_1=count_1+1 
    tempTable[count_1] = {} -- Multi Dimensional Array 
    local count_2 = 0 
    for word_ in string.gmatch(myTable[#myTable], "([^,|,;]+)") do 
     count_2=count_2+1 
     local str_ = word_ 
     tempTable[count_1][count_2] = str_ 
     --print(count_1.."|"..count_2.."|"..str_) 
    end 
end 

print("------------------------") 
local myTable = {} -- Resetting my table, just for using it again :) 
for i=1,count_1 do 
    for j=1,#tempTable[i] do 
     print("tempTable["..i.."]["..j.."] = "..tempTable[i][j]) 
     if(j==1)then myTable[i] = tempTable[i][j] end 
    end 
end 

print("------------------------") 
for i=1,#myTable do 
    print("myTable["..i.."] = "..myTable[i]) 
end 
--[[ So now you will have a multidimensional array tempTable with 
    elements as: 
     tempTable = {{House,home,flat}, 
        {40}, 
        {Bed,bunk}, 
        {20}, 
        {Mirror}, 
        {10}} 

    So you can simply take any random/desired value from each. 
    I am taking any of the 3 from the string "House,home,flat" and 
    assigning it to var1 below: 
--]] 

var1 = tempTable[1][math.random(3)] 
print("var1 ="..var1) 

-- So, as per your need, you can check var1 as: 
for i=1,#tempTable[1] do -- #tempTable[1] means the count of array 'tempTable[1]' 
    if(var1==tempTable[1][i])then 
    print("Ok") 
    break; 
    end 
end 

---------------------------------------------------------------- 
    -- Here you can print myTable(if needed) -- 
---------------------------------------------------------------- 
for i=1,#myTable do 
    print("myTable["..i.."]="..myTable[i]) 
end 

--[[ The output is as follows: 
    myTable[1]=House 
    myTable[2]=40 
    myTable[3]=Bed 
    myTable[4]=20 
    myTable[5]=Mirror 
    myTable[6]=10 

    Is it is what you are looking for..? 
]]-- 

---------------------------------------------------------------- 

保持編碼..... ........ :)

+0

非常感謝!這是一個很好的基礎 – Nick21

+0

如果我想插入一些特定的變量的話?例如:varA = House,varA1 = 40,varB = Bed ecc?我所擁有的變量的最大數目是七個單詞和七個數字( 現有變量)。 – Nick21

+0

@ Nick21Android:您可以將其指定爲:varA = myTable [1]; varA1 = myTable的[2];等等... –