How do I get Letters from a String based on their Position?

What I’m trying to achieve:
I am trying to get the letters of a string based off their position in the string.

image

From the example above, lets say I want the 6th and 7th letter in the string. I would like for the script to return those two letters (Which would be L and E). How can I go about doing this in an efficient way?

use string.sub

example:

local str = "EXAMPLESTRING"

print(string.sub(str, 6, 7)) -->> LE

1 Like

Awesome, Thank you!
Great job on the quick and speedy response.

1 Like

What if I wanted to replace those two letters with something like “XY” instead of “LE”?
Sorry for replying again, I forgot the whole second half of what I was trying to do.

you can use string.sub and string.gsub combined to do that

example:

local ExampleString = "EXAMPLESTRING"
local ReplaceWith = "XY"

local LettersToReplace = string.sub(ExampleString, 6, 7) -->> LE
local ReplacedString = string.gsub(ExampleString, LettersToReplace, ReplaceWith) 

print(ReplacedString) -->> EXAMPXYSTRING

this might be a bit more complicated than string.sub

1 Like

Yep, this is perfect. Thank you so much for your help I really do appreciate it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.