27

Given a string s in Lua:

s = "abc123"

If the string is non-empty, I want to store the first character of this string in a variable s1, else store nothing.

I've tried the following, but both return nil:

s1 = s[1]
s1 = s[0]

How can I get the first character without using external Lua libraries?

Kyle F Hartzenberg
  • 2,567
  • 3
  • 6
  • 24
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120

4 Answers4

35

You can use string.sub() to get a substring of length 1:

> s = "abc123"
> string.sub(s, 1, 1)
a

This also works for empty strings:

> string.sub("", 1, 1) -- => ""
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
22

You can also use this shorter variant:

s:sub(1, 1)
Termininja
  • 6,620
  • 12
  • 48
  • 49
1
local string_meta = getmetatable('')

function string_meta:__index( key )
    local val = string[ key ]
    if ( val ) then
        return val
    elseif ( tonumber( key ) ) then
        return self:sub( key, key )
    else
        error( "attempt to index a string value with bad key ('" .. tostring( key ) .. "' is not part of the string library)", 2 )
    end
end

local str = "Hello"
print(str[1])
x0r
  • 21
  • 1
  • 4
    I don't think the OP was asking how to implement `s[1]`, but how to get the first character. Also, could you add a bit more explanation on how the sample works, how to use it, and what the expected output should be (matching the original example)? – shader Jul 14 '20 at 20:12
-1

I made 3 functions for strings.

Also, yes, I know this post is a year old, and that it was already answered, but I still made a function for you.

(And I already know that uppercase and lowercase methods exist but I don't care.)

function toUppercase(originalString, printString)
    if printString ~= true and printString ~= false then
        printString = false
    end
    if printString == true then
        print(originalString:upper())
    else
        return originalString:upper()
    end
end

function toLowercase(originalString, printString)
    if printString ~= true and printString ~= false then
        printString = false
    end
    if printString == true then
        print(originalString:lower())
    else
        return originalString:lower()
    end
end

function toTitleCase(originalString, printString)
    if printString ~= true and printString ~= false then
        printString = false
    end
    changeString = originalString:gsub("%W%l", string.upper):sub(0)
    titleCase = changeString:sub(1, 1):upper() .. changeString:sub(2, #changeString)
    if printString == true then
        print(titleCase)
    else
        return titleCase
    end
end
  • Thank you ! I think this is helpful for many people trying to understand Lua basics :-) – Uli Köhler Apr 08 '23 at 18:50
  • StackOverflow is a platform where people try to give direct answers to the question. This answer is not clear. You show 3 functions, but only the last function does some manipulations with the first symbol, though it's complicated by other logic, thus it's not answering to the question directly. BTW, refactoring advice, if you remove the first if-blocks from every function, you will have the same functionality – Mikolasan Apr 09 '23 at 22:45