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

What is string.sub and how do you use it? [closed]

Asked by
Yeevivor4 155
10 years ago

Hi, thank you for reading. After doing some digging and trying to understand it from wiki, I'm still confused. Why? On Wiki.Roblox, it said:

Returns the substring of s that starts at i and continues until j; i and j may be negative. If j is absent, then it is assumed to be equal to the length of s. In particular, the call string.sub(s,1,j) returns s until as many characters as j, and string.sub(s, -i) returns a suffix of s with length i.

This is a really weird description and if anyone can give me a simpler description, I would be really grateful.

~Thank you for reading, Yeevivor4

Locked by Zafirua, User#5423, and theCJarmy7

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
10
Answered by 10 years ago

string.sub, sub means substring or part-of-a-string. It's basically taking a string and cutting off a piece of it. e.g.

1print("Hello") --Hello
2 
3print("Hello", 2) --Ello

In string.sub it looks like something like this: string.sub("String", 2,4). string.sub is you know, string.sub. In the parentheses you have your string. "Hi", "Text", "string". Next you have a number. If it's just one number, that means anything before this number will be cut off. string. string.sub("String", 2) == "tring" --t is the 2nd number, 1 is before it so it gets cut off. If there is 2 numbers(you can only have 1 to 2 numbers) then it will cut off anything before the 1st number and anything past the 2nd number. string.sub("String", 2,4) == "tri" --ng and S get cut off. If you're wondering how do we know what numbers to use, lets say we have this weird string that has a message. "afakwshellosadffgafsadsadfge" we want it to say "hello". string.sub("afakwshellosadffgafsadsadfge", 6,11) --the 6th character in the string is "s". Everything before it(afakw) also get's deleted from the string. Now the 11th character is "s" again coincidentally. "s" and everything AFTER s is deleted(adffgafsadsadfge).



Hope it helps! If you're still having trouble understanding, please tell me!


Examples of string.sub in scripts:

  1. Admin Scripts
  2. Chat service

  1. Admin
01admin = {"LordDragonZord"}
02 
03game.Players.PlayerAdded:connect(function(player)
04--  player.CharacterAdded:connect(function(char) --This isn't really necessary.
05        for _,name in pairs(admin) do
06            if player.Name == admin then
07                player.Chatted:connect(function(msg)
08                    if string.sub(msg, 1, 5) == "Kill/" and game.Players:FindFirstChild(string.sub(msg, 6)) then
09                        game.Players:FindFirstChild(string.sub(msg, 6)).Character.Humanoid.Health = 0
10                    end
11                end)
12            end
13        end
14--  end)
15end)
  1. Chat
01local default_color = "Blue"
02 
03game.Players.PlayerAdded:connect(function(player)
04    player.Chatted:connect(function(msg)
05        if string.sub(msg, 1,2) == "R/" then
06            game:GetService("Chat"):Chat(player.Character.Head,string.sub(msg,3),"Red")
07        elseif string.sub(msg, 1,2) == "B/" then
08            game:GetService("Chat"):Chat(player.Character.Head,string.sub(msg,3),"Blue")
09        elseif string.sub(msg, 1,2) == "G/" then
10            game:GetService("Chat"):Chat(player.Character.Head,string.sub(msg,3),"Green")
11        else
12            game:GetService("Chat"):Chat(player.Character.Head,msg,default_color)
13        end
14    end)
15end)
0
Oh! That's super helpful! I was confused with chat function and you saying this plus showing the admin just helped me, even though I never had a question, lol alphawolvess 1784 — 10y
0
Why not use the 'string:sub(number,number)' method? In my opinion, it's easier, also a lot easier to read and write, but also reduces some-what code space. :) TheeDeathCaster 2368 — 10y
0
Okay :3 EzraNehemiah_TF2 3552 — 10y
0
You should edit this post with some bolding and some more paragraphs then lock it. This has a really clear concise answer that many people will find useful. DigitalVeer 1473 — 10y
0
@ DigitalVeer , this is too old anyway... EzraNehemiah_TF2 3552 — 10y
Ad