How to find a repeating word in a string but replace it once

okay so if i had this string: “dog dog dog”

I want to find “dog” in it
and obviously it will find dog in it 3 times
but instead of replacing it 3 times how do I only replace 1 instance of “dog” with " " aka nothing

I honestly don’t know where to start

2 Likes

You could try using string.gmatch() for this. The following example will print a table by separating every value from a string with spaces in it.

local str = "abc aaa bbb" 
local out = {} 

for m in string.gmatch(str, "%w+") do 
     table.insert(out, m) 
end 
print(out) --{abc, aaa, bbb}

You could then remove or replace the first value easily and turn the entire table back into a string, or anything you might want to do with this.

1 Like

Well first I would convert the string to a table using string.split. Then I would replace the first instance of the word and breaking when I find a match:

local myString = "I like dog dog dog"

local function buildString(String)
     local stringTable= string.split(String, " ")  -- Creates a new index in the table for each space, returns {“I”, “like”, “dog”, “dog”, “dog”}

      for i, word in pairs(stringTable) do -- Loop through table
          if table.find(stringTable, word, i + 1) then -- Checks if the current word is found in the table, starting from the next index in the table (see string library in roblox api)
              table.remove(stringTable, i) -- If there’s a match, remove it from the table
              break -- Break to prevent additional removing
          end
      end

      local newString = ""
      for i, word in pairs(stringTable) do
           newString = newString.. " "..word -- Creates a new string (it just took out the first appearance of any repeated word)
      end

      return newString
end 

print(buildString(myString)) -- Should print “I like dog dog”

This is probably not the most efficient way of doing this.

2 Likes

Im kinda confused on how this works
Where do I tell it what word to look out for?

I added comments so you can see what I’m doing in each line. It’s not looking out for any specific word, but it searches for any word that is repeated.

oh well the string im using will have different words that are repeated so the string will more look like this “cat cat dog cat rat dog dog rat dog”

im trying to only look for repeated "dog"s

Well in that case, you can just check to see if the word in the current iteration is “dog” by just using an and if statement:


local myString = "I like dog dog dog"

local function buildString(String)
     local stringTable= string.split(String, " ")  -- Creates a new index in the table for each space, returns {“I”, “like”, “dog”, “dog”, “dog”}

      for i, word in pairs(stringTable) do -- Loop through table
          if table.find(stringTable, word, i + 1) and word.lower() == “dog” then -- Checks if the current word is found in the table, starting from the next index in the table (see string library in roblox api)
              table.remove(stringTable, i) -- If there’s a match, remove it from the table
              break -- Break to prevent additional removing
          end
      end

      local newString = ""
      for i, word in pairs(stringTable) do
           newString = newString.. " "..word -- Creates a new string (it just took out the first appearance of any repeated word)
      end

      return newString
end 

print(buildString(myString)) -- Should print “I like dog dog”

local myString = "I like dog dog dog"

local finalString = myString:match(".+dog "):gsub("dog ", "") .. myString:match("dog (.+)")

print(finalString) -- "I like dog dog"

You can use string.gsub and specify the maximum number of substitutions to be 1 in the third argument.

local str = "dog dog dog"
local result = str:gsub("dog", " ", 1)

This would result in the first occurrence of dog being replaced with a space.

yea you accidentally did gmatch instead of gsub, wanted to let you know

gmatch would follow with an error