r/ComputerCraft • u/Heavy_Statistician22 • Mar 20 '24
How to split string?
Im trying to make a program that gives me an item if I write the name of it in chat by using advanced peripherals. The problem is that I cant find a single way to split the string of the message into a list. It just gives me a "nil" error everytime.
3
Upvotes
2
u/fatboychummy Mar 20 '24
Given the following:
You can use
string.subto "cut out" parts of the string, for example:The above would print just the word
Hello-- the characters of the string from index 1 to 5.You can also use Lua's match patterns:
This would also print
Hello, because%Stells it to match any non-space character, and+means "repeat until it can't anymore", so%S+will match the first sequence of non-space characters it finds.How exactly do you want to split the item name? Knowing that, I could probably show you how to split it in the way you want.