string/lower

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m
(replace example 2 with one that is more focused on string.lower)
Line 22: Line 22:
 
}}
 
}}
 
{{Example
 
{{Example
|Description=Demonstrates opening a menu when !openmenu is typed in chat.
+
|Description=Demonstrates a common use for string.lower - case-insensitive user input.
|Code=local function myMenu()
+
|Code=-- All keys in this table must be lowercase:
    local frame = vgui.Create( "DFrame" )
+
local products = {}
frame:SetSize( 400, 400 )
+
products.apple = "Buy an apple!"
frame:Center()
+
products.banana = "Buy a bunch of bananas!"
frame:MakePopup()
+
products.tomato = "There's also tomatoes."
 +
 
 +
-- This function is case-insensitive, meaning "APPLE", "apple", and "APPle" are all the same.
 +
function GetProduct(userinput)
 +
return userinput, products[string.lower(userinput)]
 
end
 
end
  
hook.Add( "OnPlayerChat" , "AnyName" , function( ply, text )
+
-- Demonstration:
    if ply == LocalPlayer() then -- Makes sure its the local player
+
print(GetProduct("apple"))
        if string.lower( text ) == "!openmenu" then -- Checks if the player had wrote a chat message saying !openmenu
+
print(GetProduct("Apple"))
            myMenu() -- Calling the local function to start
+
print(GetProduct("APPLE"))
        end
+
print()
    end
+
print(GetProduct("banana"))
end )
+
print(GetProduct("BaNaNa"))
|Output=When you type !openmenu in chat, the DFrame pops up.
+
|OutputFixedWidth=Fixed width
 +
|Output=apple Buy an apple!
 +
Apple Buy an apple!
 +
APPLE Buy an apple!
 +
 
 +
banana Buy a bunch of bananas!
 +
BaNaNa Buy a bunch of bananas!
 
}}
 
}}

Revision as of 18:20, 22 November 2016

 string.lower( )

Contents

Description

Changes any upper-case letters in a string to lower-case letters.

Arguments

string str

The string to convert.

Returns

string

A string representing the value of a string converted to lower-case.

Examples

Example

Demonstrates the use of this function.

print( string.lower( "ABCDEFG" ) )
print( string.lower( "AbCdefG" ) )
print( string.lower( "abcdefg" ) )
print( string.lower( "1234567890" ) )

Output:


abcdefg
abcdefg
abcdefg
1234567890

Examples

Example

Demonstrates a common use for string.lower - case-insensitive user input.

-- All keys in this table must be lowercase:
local products = {}
products.apple = "Buy an apple!"
products.banana = "Buy a bunch of bananas!"
products.tomato = "There's also tomatoes."

-- This function is case-insensitive, meaning "APPLE", "apple", and "APPle" are all the same.
function GetProduct(userinput)
	return userinput, products[string.lower(userinput)]
end

-- Demonstration:
print(GetProduct("apple"))
print(GetProduct("Apple"))
print(GetProduct("APPLE"))
print()
print(GetProduct("banana"))
print(GetProduct("BaNaNa"))

Output:

apple	Buy an apple!
Apple	Buy an apple!
APPLE	Buy an apple!

banana	Buy a bunch of bananas!
BaNaNa	Buy a bunch of bananas!
Personal tools
Navigation