Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to tell if the last character in a string is a space?

Trying to find out if the last character in a string is a space character, but I'm not sure why what I have isn't working.

I have this string manipulation code, but I can't get it to work. Can someone tell me why?

print (text:sub(text:len() - 1) == (" "))

3 answers

print(text:sub(text:len()) == " ")

Without the -1 and with correct brackets. string.sub gets the selected character AND everything past it.

2 comments

0

Still returns false ;/

0

Can I see the whole script?

Hey Scriptecx,

Your problem has quiet a simple fix. In Roblox Lua, the substring parameter isn't 1 index ahead of the letter like it is in Java or some of the other languages. You just need to delete the -1. With the -1, you're getting the last 2 characters in the string. Which, will never be equal to " ".

##Well, I helped, and have a nice day/night.

~~ KingLoneCat

local str = " H$l10, this is a mix3d - str!ng. "
-- '$' anchores to the end of the string. '.' matches any character.
local last_char = str:match('.$')
local is_space = last_char == " " or false
print(last_char, is_space)

This discussion is preserved as it was. New answers, comments, and votes are closed.