Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is it Possible to Tell How Many Letters/Numbers a Value/String contains?

Asked by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

Title pretty much explains it. I believe you can use :gmatch and compare it to a tables with numbers/letters, but I was wondering if there was a more efficient way?

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First:

To just get the length of a string str, you could use either #str or str:len() or string.len(str).


Let's say you wanted to count how many digits a string contains. We could count them:

function isDigit(s)
    -- lots of ways to do this:
    return tostring(s)
    return ("0123456789"):find(s)
    return s:match("%d")
    -- (in reality you would probably just put this line
    -- into that if -- unless you were doing this
    -- all over the place)
end



local str = "1apple2banana3"
local count = 0
for i = 1, #str do
    if isDigit(str:sub(i, i)) then
        count = count + 1
    end
end
print(count) -- 3

This is a bit long and annoying, though.

Observation: we are counting 1 for each digit character, and ignoring all other characters. Thus, if we had digits string which was only the digits of str, then we would just have to find its length:

local str = "1apple2banana3"
local digits = str:gsub("%D", "")
-- global substitution (find&replace)
-- of all not digits ("%D") with nothing ("").
print(#digits) -- 3

You can do this with more complicated patterns, too. In general it might sometimes be easier to write what is something rather than what is not (e.g. using "%d" over "%D"). In that case we just need to subtract the difference:

local str = "1apple2banana3"
local notDigits = str:gsub("%d", "")

-- #digits  +  #notDigits  =  #str
-- #digits  =  #str  -  #notDigits
print( #str - #notDigits ) -- 3

For other sorts of matches, look up information about string patterns. "%a" is letters, "%A" is not letters, etc.

0
As always, big help. Shawnyg 4330 — 9y
0
local numLetters=0;for Letter in String:gmatch'%a' do numLetters=numLetters + 1; end; Diitto 230 — 9y
0
Thank you for that. Shawnyg 4330 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Yes, if you use string.len you can find the number of characters/numbers a string may contain. For example:

str = string.len("I am a string") -- Looking for the length of this string.

print(str) ==> 13

Please note that spaces count.

0
That's not what he's asking for. EzraNehemiah_TF2 3552 — 9y
Log in to vote
0
Answered by
Discern 1007 Moderation Voter
9 years ago

This could be considered as a repeat of my last answer, but string.len should help you.

myString = "THIS IS A STRING WITH A RANDOM NUMBER OF CHARACTERS!!!!!" --This string actually has 56 characters. I counted it, lol.

print(string.len(myString))

This script should print 56 in the output. If it puts some other number, then I must've counted wrong, but it should turn out to be 56 (or the actual number of characters in the string).

If I helped you, make sure to hit the Accept Answer button below my character!

0
That's not what he's asking for. He's asking for number, string, etc. values. EzraNehemiah_TF2 3552 — 9y
0
"How many letter/numbers are in a value/string?" This returns the amount of characters (or letters/numbers) in a string. Discern 1007 — 9y
0
Lets say it isn't a string but a number instead. You can't do string.len() on 23. He needs to convert the number into a string. He used gmatch but he needs to find a better way to do so, because gmatch just replaces the characters. EzraNehemiah_TF2 3552 — 9y
0
string.len() does work on number values. I just tried print(string.len(25702)) without quotation marks, and it printed 5 in the output. Discern 1007 — 9y
View all comments (4 more)
0
Hm... Well He needs a table. Also what if he doesn't want the spaces... You could probably do that with string.byte() since the byte for space is 32. EzraNehemiah_TF2 3552 — 9y
0
Hmm, interesting. We need BlueTaslem for this, cuz he always knows :D Discern 1007 — 9y
0
You're at 99 rep. EzraNehemiah_TF2 3552 — 9y
0
I apologize if I was a bit broad. BlueTaslem did answer it, as I was looking how to differentiate them, such as "123abc123" Shawnyg 4330 — 9y

Answer this question