Extract string from sentence

I don’t know much about string patterns and I’m having trouble understanding the roblox API.
Could someone explain to me how I could extract a string that could change from a sentence?

For example: A script prints a player’s name and current position. I want to extract a player’s name and print it regardless of whether or not the name changes

2 Likes

I don’t know what you mean by “from a sentence”, but couldn’t you just get the players name each time you want to print it?
local name = Player.Name
print(name)
It shouldn’t matter if the name changes

Just used sentence because I don’t know what to call the print’s results. I could get the player’s name using other methods but the method I want to use is using string patterns so that I can understand how they work

Try this example:

local sentence = "hello its me star!"

local wordToFind = "star"

if string.find(sentence,wordToFind) then

print("found word")

else

print("word not found")

end

use function :
string.sub()
with first parameter as where string you want to extract starts and 2nd parameter where it ends so :
string.sub(1, 3) : (“HelloWorld”) => (“Hel”)

1 Like

This is good for letters but I want to extract entire words, is there a way I could do that?

This is useful but in my example if I wanted to extract a player’s name and they change their name, I would then also have to change the targeted word to find, is there another way for me to extract the name regardless of whether or not they change it? Or better yet, is there a way for me to extract a word based on it’s position in a sentence?

What do you mean change the players name?

you can use string.gsub for this issue!

local str = "hello i am Darobbap"
local newStr = string.gsub(str, "i am ", "")

print(newStr) --It should print "Hello Darobbap"

I mean if I were to put my example into an actual script, any person who decides to change their name would not get their name printed

This is close, is there a way for me to use this to extract only the player’s name so I only get “Darobbap” as the result?

local str = "hello i am Darobbap"
local newStr = string.gsub(str, "hello i am ", "") --The 3rd argument is the string you want to replace the 2nd arg with btw

print(newStr) --It should print "Darobbap"

But why can’t you just do player.Name?

Just wanted to understand how string patterns work

Oh i understand! If you want to learn more about strings just take a look at this post