String Manipulation

Learn how to manipulate strings!

by xJDiviisionZ

Author Avatar

string.byte()

This operator allows you to get the byte value of a character. For example, the letter A has a byte value of 97.

If I was to use this function, it would look like this:

string.byte('a')

This would then return:

=> 97

string.char()

On the other hand, we have string.char()! This is the exact opposite of string.byte(). If I were to input an integer value, I would get a character returned (provided the integer was valid):

string.char(97)

This would then return:

=> 'a'

string.find()

If you have a string and you are looking to find if the string contains a set of characters - in order - then you can use this to do so. Using this function looks like this:

string.find('asd2383t98wertodfcvjbxcv', '383')

In this case, it would return:

=> 7 -- Because that is where the '383' string ends in the first parameter

string.format()

Now this one is a little bit harder to wrap your head around as you can have many different operators. However, I will show you the simplest way to use it. Using the function:

string.format('Hi my name is %s and I like %s', 'Jordan', 'pineapples')

This would then return:

=> 'Hi my name is Jordan and I like pineapples'

string.len()

Simply put, this returns the length of a string. To use it, I simply write:

string.len('one')

Which will return:

=> 3

string.lower()

In basic terms, this converts the whole string to lowercase characters. To use it, I would write:

string.lower('HeLlO MY NAME is JoRdaN')

Which will return:

=> 'hello my name is jordan'

string.upper()

As you can probably guess, it's the opposite of string.lower()! To use this function, I write:

string.upper('hello my name is jordan')

And I will get:

=> 'HELLO MY NAME IS JORDAN'

Edits

[1]: I noticed a comment asking what string.sub() is. Here is how you use it:

Say we had stored a value in a table. Perhaps it was a requirement before you could buy something from the shop; you could store this as ("<material>-<quantity>"). For example:

local requirements = {
["1"] = ("coal-5")
}

If we were to use a for loop to get each requirement (perhaps, a later tutorial), we could use 3 string functions to separate this value down. Running this code:

print(requirements["1"])

would return

=> "coal-5"

However, we can't do much with this, so we need to break it down into the material and quantity. We can do this to find the seperator (in this case, -):

local seperator = string.find(requirements["1"], "-")

Now, we need to break down the 2 parts of the string. This is where string.len() and string.sub() come in handy.

local material = string.sub(requirements["1"], 1, (seperator-1))
local quantity = tonumber(string.sub(requirements["1"], (seperator+1), string.len(requirements["1"]))

To explain it a little easier:

Strings can be broken down into "index", meaning each character has a value in the string. 1st character is 1, 2nd character is 2 and so on..

Moreover,

string.sub()

can be broken down into

string.sub(string, from, to)

What this means is that, it will return the characters between (and including) from and to.

string.sub("Hello", 1, 3)

Output:

=> "Hel"

string.sub() can be a little harder to grasp compared to the others so please spend some time with it if you need to.

View in-game to comment, award, and more!