String Manipulation

I have had little experience with string manipulation. I have looked over the developer site and I still do not understand how to accomplish what I am trying to do. How would I do something if I named something, “RedCar” or “BlueCar” but I use string manipulation to make it so that if it has the word “car” in it gets accepted.

1 Like

You could use string.find to find a specific keyword, example

local vehicle = "RedCar"

if string.find(vehicle:lower(),"car") then
    print("found car")
end

This will make the vehicle name lower case and then see if it has the word “car” in it, if you care about the capitalization, remove the lower()

You can also use string.match, but if you do not care about the return, it doesn’t matter if you use string.find or string.match (string.find returns the index at which the found pattern was located whilst string.match returns the first match in that string),

1 Like

First we make the string lowercase so it can be processed easier (it doesnt matter if the player types using caps or not) then we use String:match() to see if a specific word is located in that string

local ExampleString = "This is an example"
local LowerCaseString = string.lower(ExampleString)

if LowerCaseString:match("example") then
print("String Contains the work example")
end
-- prints the amount of letters
print(string:len('DevForum')) --8
-- splits string by the first parameter in to words
local words = string:Split("_", "you_will_be_a_pro_dev")-- returns a table
--                                1    2   3 4  5   6
print(words[6])-- dev


Thanks for the help. I applied this to the actual situation I was in and it worked. Thanks

What is string.split? I don’t know the other functions of string

1 Like

Here is the documentation you are seeking: string (www.developer.roblox.com)

Contrary to how everyone else has attempted to manipulate strings in this thread, it is important to remember that strings are not objects in Lua and therefore can’t have any associated methods. This means you cannot use the “:” operator on them. Instead, you need to call a function of the string library and pass in the reference to the string you would like to manipulate.
Edit: My knowledge was lacking!

Here’s how you might accomplish your goal:

local listOfString = {
	"RedCar",
	"BlueCar",
	"PinkTruck",
	"careful",
	"anchor",
	"cavalry"
}

local match = "car"

for _, str in pairs(listOfString) do
	
	--[[
		Call string.lower on both the str being checked and the match string to ensure
		capitalization does not impact the search
	--]]
	local lowercase_str = string.lower(str)
	local lowercase_match = string.lower(match)
	local foundAt, foundEnd = string.find(lowercase_str, lowercase_match)
	if foundAt then
		print(str, "had occurence of", match)
	end
	
end
1 Like

string.split basically justs returns a table of strings that have been split using a certain separator, example

local text = "This is a text"

local splits = text:split(" ")
print(splits[2]) --Prints is

@East98 You can actually use the : operator on them, such as here as an example

image

@ZINTICK That’s what I had already stated

1 Like