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

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

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?

1 comment

0

I tought you want to know how many spaces or the lenght. No the blank spaces. Sorry for get confused

2 answers

✓ Accepted answer

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!

5 comments

0

This just finds the length of the string, it doesn't find the amount of spaces that it has.

0

Oh, you can't do that. Sorry, only you can know the lenght of the string.

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.

0

If I find it I'll notify you.

0

I make it. Your welcome.

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