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

string.upper case not detecting when the string is upper case?

function theissue(l)
	--yea basically this wont detect if the string is upper case or lower case i have no idea what im doing wrong here
	local typ = ""
if string.upper(l) then
	typ = "upper"
else
	typ = "lower"
end
print(typ)
end

Basically it wont detect if the string is upper case i dont know what i am doing wrong but apparently im doing something wrong i guess

3 comments

0

else is not necessary

0

for one, dont make the name of the first parameter "1", the lua parser is going to see that as a number, and a variable name

0

thats actually the letter L

2 answers

✓ Accepted answer

string.upper() only just returns the string with all the letters in uppercase, it doesnt return if theres uppercase nor lowercase letters inside the string. you'd use string.match() and the %u pattern.

local function theissue(L) -- use local functions
	local typ

	if string.match(L, "%u") then
		typ = "upper"
	else
		typ = "lower"
	end
end

though this would only detect if theres a letter thats uppercase, not the entire string. if you'd want to find out if the entire string is uppercase then you could loop through it.

local function theissue(L) 
	local typ = "upper"  

	for letter in string.gmatch(L, "%a") do 
		if not string.match(letter,"%u") then -- if it finds a lowercase letter
			typ = "lower" 
			break -- breaks the loop
		end
	end

	print(typ)
end

the %a pattern is every letter in a string.

3 comments

0

thank you it works but how do i do the thingy that it says answered

0

its under the answer?

0

thanks

Here's a function to check if it is uppercase.

function IsUpper(s)
	return (s == string.upper(s));
end

4 comments

0

why the semicolons

0

It's required to end a statement with it in alot of languages and is good practice to end a statement with it even if the language doesn't require it.

0

In Lua it's considered bad practice by some, but it's mostly personal preference, personally I think it looks neater but I don't tend to use it ;p

0

Yeah, it's just for neatness and to make the code easier to read, but I'd never say that it's bad practice to use them.

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