在sqlite上拆分一个字符串

时间:2013-10-19 15:35:49

标签: string sqlite corona

我不知道sqlite但我必须实现已经完成的数据库。我正在使用Corona SDK进行编程。问题:我有一个名为“答案”的列,格式为:House,40|Bed,20|Mirror,10 ecc。 我想拆分字符串并删除"," "|",如下所示:

VARIABLE A=House

VARIABLE A1=40


VARIABLE B=Bed

VARIABLE B1=20

VARIABLE C=Mirror

VARIABLE C1=10

对不起我的英语。感谢大家。

1 个答案:

答案 0 :(得分: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..?
 ]]--

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

继续编码.............:)

相关问题