Jump to content

Detect if string contain spaces


Recommended Posts

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! :D

Link to comment

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
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 wrong

It was an function to detect if a string have only one space D:

If it gote more then one of 0 then return false xD

Link to comment
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
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...