This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Ch1ck3nL1v3r's profile picture

Check if string contains another string?

Started by Ch1ck3nL1v3r, 23 February 2015 - 03:37 PM
Ch1ck3nL1v3r #1
Posted 23 February 2015 - 04:37 PM
I have done some googling and cannot find any way of checking whether "string1" contains "string2",

an example of what I want to do is checking whether "the word cat is within this sentance" contains "cat"

please help! :blink:/>
MKlegoman357 #2
Posted 23 February 2015 - 04:51 PM
string.find is your friend:


if string.find( "the word cat is within this sentence", "cat" ) then
  --# that sentence does contain the word cat
end

Make sure to read on the string library here. Most of the time you'll find your answer on Google, you just have to search it right. For example, in your situation I would be googling 'lua check if string contains another string', which brings up some good pages for this problem. But if google doesn't help you then go ahead and post here :)/>
Dog #3
Posted 23 February 2015 - 04:52 PM
For that, you would use the String API.

e.g.

local stringA = "the word cat is within this sentence"
local wordIsThere = string.find(stringA, "cat")
if wordIsThere then print("Found cat!")

string.find will return the position of the word 'cat' within stringA, so wordIsThere would be set to 10 (since cat starts at the 10th position in the sentence).

The third line of the example is where you determine if you've found what you're looking for. Since wordIsThere has a value (in this example it is 10), it evaluates to true and the above example prints "Found cat!". If we had searched for 'dog', wordIsThere would not have a value assigned to it and would evaluate to false, and the example wouldn't print anything.

In this second example, we'll search for 'dog'…

local stringA = "the word cat is within this sentence"
local wordIsThere = string.find(stringA, "dog")
if wordIsThere then
  print("Found dog!")
else
  print("Didn't find dog...")
end

Since 'dog' isn't found, wordIsThere has no value assigned and evaluates to false. Since wordIsThere evaluates to false, the example will print "Didn't find dog…"

EDIT: :ph34r:/>'d
Edited on 23 February 2015 - 03:56 PM
InDieTasten #4
Posted 23 February 2015 - 04:53 PM
string.find will offer you start and end position within the given string. But you have to be careful with adding special characters, as they might be interpreted as patterns.

start, finish = string.find("the word cat is within this sentance", "cat")
if(start) then
--it does contain it
else
--it doesn't contain it
end
Hating it, when I miss the warning of someone else already posting answer -.- ^^
Edited on 23 February 2015 - 03:55 PM
Ch1ck3nL1v3r #5
Posted 23 February 2015 - 04:58 PM
string.find will offer you start and end position within the given string. But you have to be careful with adding special characters, as they might be interpreted as patterns.

start, finish = string.find("the word cat is within this sentance", "cat")
if(start) then
--it does contain it
else
--it doesn't contain it
end
Hating it, when I miss the warning of someone else already posting answer -.- ^^
For that, you would use the String API.

e.g.

local stringA = "the word cat is within this sentence"
local wordIsThere = string.find(stringA, "cat")
if wordIsThere then print("Found cat!")

string.find will return the position of the word 'cat' within stringA, so wordIsThere would be set to 10 (since cat starts at the 10th position in the sentence).

The third line of the example is where you determine if you've found what you're looking for. Since wordIsThere has a value (in this example it is 10), it evaluates to true and the above example prints "Found cat!". If we had searched for 'dog', wordIsThere would not have a value assigned to it and would evaluate to false, and the example wouldn't print anything.

In this second example, we'll search for 'dog'…

local stringA = "the word cat is within this sentence"
local wordIsThere = string.find(stringA, "dog")
if wordIsThere then
  print("Found dog!")
else
  print("Didn't find dog...")
end

Since 'dog' isn't found, wordIsThere has no value assigned and evaluates to false. Since wordIsThere evaluates to false, the example will print "Didn't find dog…"

EDIT: :ph34r:/>'d
string.find is your friend:


if string.find( "the word cat is within this sentence", "cat" ) then
  --# that sentence does contain the word cat
end

Make sure to read on the string library here. Most of the time you'll find your answer on Google, you just have to search it right. For example, in your situation I would be googling 'lua check if string contains another string', which brings up some good pages for this problem. But if google doesn't help you then go ahead and post here :)/>

Wowza, 3 replies at once. The rare double ninja :D/> . Cheers for the help!