How would I get the first character of a string

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to get the first letter of a text

  2. What is the issue? Include screenshots / videos if possible!
    The issue is I dont know how to get it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked at the developer hub and tried gsub, sub, split, and len
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
    Heres a example: " Hello World" to “Hello World”

1 Like

Just use string.sub() or its variant String:sub().

print(("Hello"):sub(1, 1)) -- H
4 Likes
local someString = "abc"
local firstLetter = string.match(someString, "^.")
print(firstLetter)
4 Likes

I tried that but I wanted to get the first letter of the text, like say I put alot of spaces before a string then put a word after the string, I want to remove all of the spaces before the first letter of the string. How would I do that?

1 Like

You can use gsub for that:

local String = string.gsub(STRING_NAME, "%s+", "")
3 Likes

just use @Limited_Unique‘s code and modify it a bit

local someString = "    abc"
local firstLetter = string.match(someString, "^%s*(.)")
print(firstLetter)

now it works with spaces

2 Likes