Jump to content




[Question] Getting A Certain Word From A String?


  • You cannot reply to this topic
4 replies to this topic

#1 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 01 August 2013 - 03:01 AM

Hi there! I'm wondering if you can get a certain word from a string like get the word 'hello' from 'hello world'. I know you can.. But how?

Thanks. // Mackan90096

#2 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 01 August 2013 - 03:07 AM

I assume you mean you want to find the position of a word in a string. Use string.find(str, pattern) Example:

string.find("hi how are you", "how") => 3 5

#3 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 01 August 2013 - 03:09 AM

Well, not really... I want to check like if you type 'hi there' I want it to check whats after 'hi' ...

#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 01 August 2013 - 03:29 AM

local function whatsAfterMyWord(text,found)
  local words = {}
  local position = 0
  for _,word in string.gmatch(text,"[^ \t]+") do
    words[#words+1] = word
  end
  for k,v in pairs(words) do
    if v == found then 
      position = k 
    end
  end
  if position == 0 then error("word not found",2) end
  if words[position + 1] == nil then
    error("no word after your input",2)
  end
  return words[position + 1]
end
Should work :)/>
It iterates trough all words in the text and saves the position of you entered word. At the end it returns the word after your entered word.

#5 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 01 August 2013 - 03:32 AM

View PostFreack100, on 01 August 2013 - 03:29 AM, said:

local function whatsAfterMyWord(text,found)
  local words = {}
  local position = 0
  for _,word in string.gmatch(text,"[^ \t]+") do
	words[#words+1] = word
  end
  for k,v in pairs(words) do
	if v == found then
	  position = k
	end
  end
  if position == 0 then error("word not found",2) end
  if words[position + 1] == nil then
	error("no word after your input",2)
  end
  return words[position + 1]
end
Should work :)/>
It iterates trough all words in the text and saves the position of you entered word. At the end it returns the word after your entered word.

Thanks!

I'll try it!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users