My string is
text1,text2
I want to split text1 and text2 by using the ','.
Try this:
s="text1,text2"
t1,t2=s:match("(.-),(.-)$")
print(t1,t2)
To get an iterator with the substrings, you can call string.gmatch.
for i in string.gmatch(example, "%P+") do
print(i)
end
To just get them into two separate strings, you can just call the iterator;
> iter = string.gmatch(indata, "%P+")
> str1 = iter()
> str2 = iter()
> print (str1)
test1
> print (str2)
test2
If you want them stored in an array instead, there's a whole discussion here how to achieve that.
#lhf added a better pattern [^,]+ in the comments, mine splits on any punctuation, his only on comma.
Try the functions given in this page:
http://lua-users.org/wiki/SplitJoin
Related
my idea is the convert the 2 strings into byte, subtract and then check if they're 0 using a for loop like this
function match(str1, str2, callback)
local res = string.byte(str1) - string.byte(str2)
for i = 1, res(0) do
spawn(callback)
end
end
but that just doesn't work can anyone write me a code would appreciate...
rawequal(str1, str2) -- Compares two values without calling any metamethods
What I'm looking for is something like the following, but it only applies to the first find it gets.
str:gsub("1", "")
I'd like it to only delete the first 1 it finds OR just the first word of the string.
How would I go about doing this?
try this:
local str = "234243 232564 se42"
local str, i = str:gsub("1", "",1)
print (str,i)
str = (i>0) and str or str:gsub("^.-%s", "",1)
print (str)
only when there are spaces in the string (more than one word).
1.how to remove second AMPERSAND from following string?
"apple&banana&grape&apple"
2.how to split the string at second AMPERSAND for following string? I want to ignore the first AMPERSAND and split from second AMPERSAND onwards.
"apple&banana&grape&apple"
arr = "apple&banana&grape&apple".split('&')
#arr = ["apple", "banana", "grape", "apple"]
To solve first query,
arr[0..1].join('&').concat(arr[2..-1].join('&'))
# "apple&bananagrape&apple"
For second query,
[words[0..1].join('&'), words[2..-1]].flatten
#["apple&banana", "grape", "apple"]
You can use gsub with a block:
For your first case:
index_to_remove = 2
current_index = 0
my_string = "apple&banana&grape&apple"
my_string.gsub('&') { |x| current_index += 1; current_index == index_to_remove ? '' : x}
#=> "apple&bananagrape&apple"
For your second case, you can replace the second & with a unique value and split on that:
index_to_split = 2
current_index = 0
my_string = "apple&banana&grape&apple"
my_string.gsub!('&') { |x| current_index += 1; current_index == index_to_split ? '&&' : x}
my_string.split('&&')
#=> ["apple&banana", "grape&apple"]
If you split the string by the &, it's quite simple to rejoin the first two / last two and convert back into a string or array:
words = "apple&banana&grape&apple".split('&')
# First query
words[0..1].join('&') + words[2..-1].join('&')
# Giving you: "apple&bananagrape&apple"
# Second query
[words[0..1].join('&'), words[2..-1].join('&')]
# Giving you: ["apple&banana", "grape&apple"]
You could tweak this for different length strings and separators as needed.
I imagine there's a good solution using regex matchers, but I'm not too hot on them, so hope this helps in some way!
I have a string which consists of numbers:
str = "1234567892"
And I want to iterate individual characters in it and get indices of specific numbers (for example, "2"). As I've learned, I can use gmatch and create a special iterator to store the indices (because, as I know, I just can't get indices with gmatch):
local indices = {}
local counter = 0
for c in str:gmatch"." do
counter = counter + 1
if c == "2" then
table.insert(indices, counter)
end
end
But, I guess, this is not the most efficient decision. I also can convert string to table and iterate table, but it seems to be even more inefficient. So what is the best way to solve the task?
Simply loop over the string! You're overcomplicating it :)
local indices = {[0]={},{},{},{},{},{},{},{},{},{}} --Remove [0] = {}, if there's no chance of a 0 appearing in your string :)
local str = "26842170434179427"
local container
for i = 1,#str do
container = indices[str:sub(i, i)]
container[#container+1] = i
end
container = nil
To find all indices and also do not use regexp but just plain text search
local i = 0
while true do
i = string.find(str, '2', i+1, true)
if not i then break end
indices[#indices + 1] = i
end
How can I convert a string like s = "6.1101,17.592,3.3245\n" to numbers in Lua.
In python, I usually do
a = s.strip().split(',')
a = [float(i) for i in a]
What is the proper way to do this with Lua?
This is fairly trivial; just do a repeated match:
for match in s:gmatch("([%d%.%+%-]+),?") do
output[#output + 1] = tonumber(match)
end
This of course assumes that there are no spaces in the numbers.