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

What is the best method of getting specific lines in a string ?

For example,

local a = "Hello and good day to you!"

local b = "Hi there and good day to you too!"

(some form of detection)

Line 5 from string a = "to"

Line 2 from string b = "there"

I would love someone to answer how to detect the specific words!

1 comment

0

string.find()

2 answers

✓ Accepted answer

You can use string.gmatch to iterate over the words in the string.

local function GetWord(str, n)
	local count = 0
	for word in str:gmatch("%w+") do
		count = count + 1
		if count == n then
			return word
		end
	end
end

%w+ will capture runs of alphanumeric characters, which are the "words" being counted. GetWord returns the nth "word" in str.

3 comments

0

orrrrrrrrrrr string.find()

0

I think you interpreted the question entirely differently, your answer is not an alternative to mine.

0

1wafflel, thanks!

This is very simple, just use string.find():

local string = "Hello World!"
local s_find = string.find(string, "Hello")

The found part of the string can be used for whatever is needed with the string.

2 comments

0

But I'm on about locating specific parts of the string, not specific words.

0

Let me rephrase, I'm on about a function to locate specific words based on lines of the string such as "string.find(string, 2) won't fine "World! will it?

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