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

How to figure out how many spaces are in a string?

Asked by 5 years ago

I want to figure out how many words are in a string but to do this I would guess that I need to figure out how many ' ' I have within the text. I have tried things such as:

print(string.match("Hello, this is ryan and welcome to my test","e"))

However, all this has done is printed the letter 'e' as that is what the string contains. Is there anything such that I can replace the letter 'e' with the amount of blank spaces there are which will also figure out the amount of words there are or will I need more script to do this task?

0
I tought you want to know how many spaces or the lenght. No the blank spaces. Sorry for get confused Foxy_Developer 111 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Ok, lets try it!

First Step : We need to know what we are making, and how we are making it. So first of all we need a string. If we already make a string we can start with the coding part!

Second Step : Second, we need to know how to use the "#" in a for loop. So lets try it and lets check it out.

Third Step : Enjoy coding! Always have to enjoy what are you doing! Play some music while you script or even play a game before so you will be relaxed.

Lets get into the code!

local whitespace = 0
local stringM = ("I love you so much!")

for i = 1, #stringM do
    if (string.sub(stringM, i, i) == " ") then
        whitespace = whitespace + 1
    end
end

print(whitespace)

That how you know how many spaces are in a string! Is a easy piece of code and you don't need any knowledge of difficult functions.

I hope my answer already solve your question. If it like that, plese accept my answer! That will help me a lot.

Keep scripting!

0
This just finds the length of the string, it doesn't find the amount of spaces that it has. RyanTheLion911 195 — 5y
0
Oh, you can't do that. Sorry, only you can know the lenght of the string. Foxy_Developer 111 — 5y
0
If I wanted to find the length I could just do string.len(STRING) and it is possible, just need to figure out how. RyanTheLion911 195 — 5y
0
If I find it I'll notify you. Foxy_Developer 111 — 5y
0
I make it. Your welcome. Foxy_Developer 111 — 5y
Ad
Log in to vote
0
Answered by
St_vnC 330 Moderation Voter
5 years ago

Try checking articles about string https://developer.roblox.com/en-us/api-reference/lua-docs/string

0
I have checked them, they don't have anything RyanTheLion911 195 — 5y
0
The reason why I came here. RyanTheLion911 195 — 5y

Answer this question