Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find how many letters there are in a string?

Hey guys! One of those rare questions I have, that are usually nooby. I'm trying to figure out how to print how many letters there are in a string. Here's the catch, the string will have numbers randomly. Example:

msg = "h3llo"
print(#msg:match( "%a+")) -- Only prints 1, the 'h', trying to figure out how to make it print 4.

I've tried using anchors, but that failed as well. If you have any tips, or any functions I'm unaware of, do tell me! Thanks.

4 comments

1

Is it going to be only numbers in there with letters or symbols too?

0

@LegitimatlyMe Hmm. Symbols will be in there too most likely

0

Why not just remove the symbols and numbers and do #str?

0

@LegitimatlyMe I could remove the symbols, but I need the numbers in there. I'm looking to see if the amount of numbers in the string + the amount of letters in string = length of string. That'll basically tell me that left overs are symbols.

3 answers

✓ Accepted answer

It seems you're aware of string patterns, but if other readers are interested: http://wiki.roblox.com/index.php?title=String_pattern

There are many ways to approach this problem. One way would be to use gmatch and then concatenate the string together and then finally get the string length.

I personally, like to keep code as small as I can, so the way that I'd approach it would be to use a gsub alongside a complement to get rid of anything that isn't a letter. Then I'd just get the length of whatever's returned to me.

local example = "h3llo world! I'm l33t!";
local letters = string.gsub(example, "[^%a]+", "");

print(letters, #letters);
-- output: hlloworldImlt 13

hi i've been sent by shawn to shawn to tell shawn that it works but i gotta make it look cool and legit by doing an explanation


First let's have a set string, what about

ye h1 th3r3

It has 6 letters and 3 numbers, but how do we make the script recognize what the letters are?

ez pz help i forgot how to write answers

local str = "ye h1 th3r3" --6 letters
local alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Using a vry cool variable named "alphabet", which represents the alphabet, this is also our script's way of figuring out what is a letter and what is not.


cool, now what?

are we just going to loop and hope for the best?

nah, instead, we'll use string.gsub, which returns 2 values which will come in handy for our purposes.

String.gsub(s, n, j) returns 2 values: A copy of s, with all n replaced by j

and

how many matches it detetctesd

We can use the second value

local str = "ye h1 th3r3" --6 letters
function findLtrs(str)
  local alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  local count = 0
  for i = 1,#alphabet do
    local h, c = str:gsub(alphabet:sub(i,i), "")
    count = count + c
  end
  return count
end

2 comments

0

Instead of making a variable to represent the alphabet, you could use the ASCII codes for A-Z, then decode it with string.char, makes things a lot faster :D

0

local _, count = text:gsub("%a", "") ?

So, one solution would be to make a table of all the characters you want to get from your string. Then, just print the length of the string.

msg = "h3llo"

local t={}
for x in string.gmatch(msg, "%a") do 
    table.insert(t, x)
end
print(#t)

For this, you would need to do '%a' instead of '%a+' because you want every individual character, instead of the longest match.

Hope this helps! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.