|
| 28 Jun 2014 11:54 PM |
I have 2 questions about strings.
1 How can i make a check to make sure a string has a certain number of numbers/letters/symbols in it?
2 how can i make a check to make sure a string has numbers in it?
I really need this, i dont even know if this is possible or not. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 11:57 PM |
1) string.len(STRING) or #"STRING"
2)if tonumber(STRING) then end
|
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 11:59 PM |
| Lua has a system of string patterns that can be used to accomplish this task. Search up 'Lua regex' and the first result on patterns should be a viable answer. I would post the link, but apparently posting the link to the Lua wiki is not allowed. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:10 AM |
| Thanks i looked into it and it looks way out of my range of Lua coding, could you assist me with a simple code of my first questions, it would be much apprecieated :) |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:14 AM |
As for getting the string's character length, @SenseiWarrior's method is viable, but the number check @SenseiWarrior provided will not work, as any letter will cause a return of 0.
if (string.match("yourstring11", '%d')~=nil) then -- CODE end
That should check for a number anywhere in the string, as %d finds a number, and string.match returns null in the event the pattern is not found. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:16 AM |
| http://wiki.roblox.com/index.php?title=Function_dump/String_manipulation |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:19 AM |
So, for the character length. Where would i put the length to check for. Like it needs to be 4 characters long... Its on aloop check to make sure it is 4 characters long is what i mean. So where would i out that :) string.len(STRING) or #"STRING" |
|
|
| Report Abuse |
|
|
| |
|
|
| 29 Jun 2014 12:23 AM |
if (string.len("somestring")>4) then -- CODE end
That should check if the character count is greater than 4. |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:27 AM |
Now ow could i mix those two, say i have little2bo3beep44
To make sure there is 4 numbers and there's numbers in it, but not in a certain order |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:33 AM |
if (string.match(thestring, '%d')~=nil) and (string.len(thestring)>4) then -- CODE end
Just change 'thestring' to the string your checking.
|
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:39 AM |
So erm.. This is my script it still printed even tho there was one number. I meant make sure there is 4 numbers in the code. ;)
ble = "swaeg1" --HUGE THANKS TOO :Wehttam664 Great guy ;) --Check if there is enough letters or too much ;) --[[if (string.len("somestring")>4) then -- CODE end --Check if there is numbers in the thingie ;) if (string.match("yourstring11", '%d')~=nil) then -- CODE end]] --Check if there is numbers and there is x amount of numbers ;) if (string.match(ble, '%d')~=nil) and (string.len(ble)>4) then print 'hi' end |
|
|
| Report Abuse |
|
|
| |
|
|
| 29 Jun 2014 12:44 AM |
local string = "I 1like2 pepperoni3 454 pizza90" local numbers = 0 for i = 1, #string do for o = 1, 9 do if string.sub(string,i,i)==tostring(o) then --code end end end
You could make a table for the alphabet and symbols. You could also try working with string.match() |
|
|
| Report Abuse |
|
|
|
| 29 Jun 2014 12:48 AM |
local string = "I 1like2 pepperoni3 454 pizza90" local numbers = 0 for i = 1, #string do for o = 1, 9 do if string.sub(string,i,i)==tostring(o) then numbers = numbers + 1 print(numbers) --# of numbers in string end end end |
|
|
| Report Abuse |
|
|