Headshot4Fun Posted August 30, 2011 Share Posted August 30, 2011 So. I Was wondering if theres a way to detect if a string has more then one or zero spaces. Because im Planning to make a register script. I Only need this part. exemple: "abcd efg" would return true "abcd efg hij" would return false "abcdeft" would return false too. Thanks folks! And sorry if my english is bad! Link to comment
Cadu12 Posted August 30, 2011 Share Posted August 30, 2011 I did it for you function StringMoreThanMore(str, number) local morethanmore = 0 local morethanmore2 = 0 for i=1, #str do morethanmore = morethanmore + 1 if string.sub(str, morethanmore, morethanmore) == " " then morethanmore2 = morethanmore2 + 1 end end if number == morethanmore2 then return true end return false end function testing(player, command, ...) local Spaces = { ... } local String = table.concat( Spaces, " " ) if StringMoreThanMore(String, 4) then outputChatBox("TRUE: " .. String) else outputChatBox("FALSE: " .. String) end end addCommandHandler("helps", testing) To use this: if StringMoreThanMore("Como vai?", 1) then: return true if StringMoreThanMore("Test test", 3) then: return false if StringMoreThanMore("Test test test test", 3) then: return true Link to comment
Headshot4Fun Posted August 31, 2011 Author Share Posted August 31, 2011 I did it for you function StringMoreThanMore(str, number) local morethanmore = 0 local morethanmore2 = 0 for i=1, #str do morethanmore = morethanmore + 1 if string.sub(str, morethanmore, morethanmore) == " " then morethanmore2 = morethanmore2 + 1 end end if number == morethanmore2 then return true end return false end function testing(player, command, ...) local Spaces = { ... } local String = table.concat( Spaces, " " ) if StringMoreThanMore(String, 4) then outputChatBox("TRUE: " .. String) else outputChatBox("FALSE: " .. String) end end addCommandHandler("helps", testing) To use this: if StringMoreThanMore("Como vai?", 1) then: return true if StringMoreThanMore("Test test", 3) then: return false if StringMoreThanMore("Test test test test", 3) then: return true You got it all wrongIt was an function to detect if a string have only one space D: If it gote more then one of 0 then return false Link to comment
Cadu12 Posted August 31, 2011 Share Posted August 31, 2011 ? I dont got what you mean, sorry. Im not good englishs skills if StringMoreThanMore("Testtesttest", 0) then: return true if StringMoreThanMore("Test tes ttes t2", 0) then: return false Link to comment
SDK Posted August 31, 2011 Share Posted August 31, 2011 function oneSpace(str) return not ( not string.find(str, "%s") or string.find(str, "%s.*%s") ) end oneSpace("abcd efg") --true oneSpace("") --false oneSpace(" ") --true oneSpace(" ") --false oneSpace("abcd efg hij") --false oneSpace("abcdeft") --false oneSpace("Como vai?") --true oneSpace("Test test test test") --false Link to comment
Cadu12 Posted August 31, 2011 Share Posted August 31, 2011 SDK D: You pwned me in Lua Link to comment
Headshot4Fun Posted August 31, 2011 Author Share Posted August 31, 2011 function oneSpace(str) return not ( not string.find(str, "%s") or string.find(str, "%s.*%s") ) end oneSpace("abcd efg") --true oneSpace("") --false oneSpace(" ") --true oneSpace(" ") --false oneSpace("abcd efg hij") --false oneSpace("abcdeft") --false oneSpace("Como vai?") --true oneSpace("Test test test test") --false THIS... is.... perfect D:Thanks mate! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now