Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I check the first 7 characters of a string, in a concise form?

Asked by
Mystdar 352 Moderation Voter
9 years ago

If the word is Solider how would I do it,

Without doing this:

String = "soldier1"
Name = {"s", "o", "l", "i", "d", "e", "r")
for i, in String do
    if String[i] = Name[i] then
        print (..i.. "Ok")
-- This is rough I just done it now, this isn't supposed to be accurate.

1 answer

Log in to vote
0
Answered by
Lacryma 548 Moderation Voter
9 years ago

This uses the sub method of the string class.

Change s1 and s2 to whatever you want. Also change the num variable for the number of letters you want to check.

local s1 = "soldier1"
local s2 = "soldier"
local num = 7

if #s1 >= num and #s2 >= num then
    if s1:sub(1, num) == s2:sub(1, num)  then
        -- first {num} letters are the same
    end
end
Ad

Answer this question