This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Thib0704's profile picture

String function to split string every §'s ?

Started by Thib0704, 23 February 2014 - 03:49 PM
Thib0704 #1
Posted 23 February 2014 - 04:49 PM
Hi,
I'm trying to make a function, that will change the color of the text
as soon as it finds a §<colourNum> in the string.
I'm thinking the best way to do that would be to use the String Library!

Any help would be apreciated!

Thanks
CometWolf #2
Posted 23 February 2014 - 05:00 PM

local derp = "derp§10derp"
local colorStart,colorEnd = derp:find"§%d+" --find the character number where the color definition begins and ends
print(derp:sub(1,colorStart-1) --print the non-color part
term.setTextColor(derp:sub(colorStart+1,colorEnd)) --change term color
print(derp:sub(colorEnd+1,#derp) --print color part
civilwargeeky #3
Posted 26 February 2014 - 04:08 AM
If you want, I made a proof-of-concept a while ago for this:
http://pastebin.com/PsjYkZXY

Use writeCode("%lbThis text is light blue %blThis text is black")
or print(encode("%lbThis text is light blue %blThis text is black"))

All the color codes should be the first two letters of the color, two word colors are the first letters of the words.

Hope this can help a bit. :)/>
Edited on 26 February 2014 - 03:09 AM
theoriginalbit #4
Posted 26 February 2014 - 04:43 AM
A user called Mads made something similar to this previously only using &amp; followed by 0-F to indicate colour. link.

EDIT: do note, there is a much better advantage to using a hex character directly after the delimiter since there are 16 colours and 0-15 can be represented with one character in HEX as 0-F. this definitely becomes apparently when you attempt to use the code that CometWolf suggested with the following input Foo §28Bar with the desired output of this Foo 8Bar it would instead be attempting to make `Bar` the colour 28 (which doesn't exist). Whereas making use of HEX means that you check the character directly after the § symbol and convert it to the colour number…
Hint: to convert a number from HEX to decimal
local dec = tonumber(hex, 16)
to convert a number (0-15) into a ComputerCraft colour
local col = 2^dec
Edited on 26 February 2014 - 04:08 AM