DEV Community

Cover image for Lua String Manipulation
Mrexamples
Mrexamples

Posted on

Lua String Manipulation

We are discussing Lua String Manipulation, hoping that it would help the learners. Lua provides a number of functions for manipulating strings. Some of the most common functions include:

string.len(): Returns the length of a string.
string.sub(): Returns a substring of a string.
string.upper(): Converts a string to uppercase.
string.lower(): Converts a string to lowercase.
string.gsub(): Replaces all occurrences of a substring in a string.

The string.len() function returns the number of characters in a string. For example, the following code will print the number of characters in the string "Hello, world!":

Lua
str = "Hello, world!"

len = string.len(str)

print(len)
Use code with caution. Learn more
This code will print the following output:

13

The string.sub() function returns a substring of a string. The substring is specified by two arguments: the start position and the end position. The start position is the index of the first character in the substring. The end position is the index of the last character in the substring.

For example, the following code will print the substring "Hello" from the string "Hello, world!":

Lua
str = "Hello, world!"

substring = string.sub(str, 1, 5)

print(substring)
Use code with caution. Learn more
This code will print the following output:

Hello
The string.upper() function converts a string to uppercase. For example, the following code will convert the string "hello, world!" to "HELLO, WORLD!":

Lua
str = "hello, world!"

uppercase_str = string.upper(str)

print(uppercase_str)
Use code with caution. Learn more
This code will print the following output:

HELLO, WORLD!
The string.lower() function converts a string to lowercase. For example, the following code will convert the string "HELLO, WORLD!" to "hello, world!":

Lua
str = "HELLO, WORLD!"

lowercase_str = string.lower(str)

print(lowercase_str)
Use code with caution. Learn more
This code will print the following output:

hello, world!
The string.gsub() function replaces all occurrences of a substring in a string with another substring. The string.gsub() function takes three arguments: the string to search, the substring to search for, and the replacement substring.

For example, the following code will replace all occurrences of the space character in the string "Hello, world!" with an underscore character:

Lua
str = "Hello, world!"

new_str = string.gsub(str, " ", "_")

print(new_str)
Use code with caution. Learn more
This code will print the following output:

Hello_world!

These are just a few of the many string manipulation functions available in Lua. By using these functions, you can easily manipulate strings to create the desired output.

Conclusion

Lua string manipulation is a powerful tool that can be used to modify strings. By following the examples in this forum post, you can learn how to use these functions to manipulate strings in Lua.

Top comments (0)