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

how to get list of characters in a string? [SOLVED]

Asked by 5 years ago
Edited 5 years ago

The title is pretty self explained, i need to get every character in a string, including numbers and special characters, and letters. What i'm trying to do is check if a text box has a letter or special character, 'cause i only want it to have numbers.

0
use string.match() to find a word or character inside a string, if thats what ur looking for User#23365 30 — 5y
0
Thanks! KinqAustinn 293 — 5y

1 answer

Log in to vote
0
Answered by
ozzyDrive 670 Moderation Voter
5 years ago

You can create an array of all the characters using the iterator returned by string.gmatch when calling it with the . class that represents any character.

local s = "hello world"
local t = {}

for c in s:gmatch(".") do
    table.insert(t, c)
end

Alternatively you can use numerical for.

Though, this isn't the optimal way to solve your problem, unless you really want to do something with all the characters. You can use the string.match function to grab the numerical part of the input. Then simply replace the current text with that numerical string.

textBox:GetPropertyChangedSignal("Text"):Connect(function()
    textBox.Text = textBox.Text:match("%d+") or ""  --  if no chain of digits was found, replace the text with an empty string to avoid the API from throwing an error when attempting to apply "nil" to a property that expects a string
end
Ad

Answer this question