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

/ in strings

Started by flaghacker, 25 May 2014 - 05:31 PM
flaghacker #1
Posted 25 May 2014 - 07:31 PM
I want to do something like this:

local text = "/texthere\"
but it errors with "unfinished string", because the \ skips the next ". How do I fix this?
Edited on 25 May 2014 - 05:31 PM
Lignum #2
Posted 25 May 2014 - 07:38 PM
Escape the backslash with another backslash.
local text="/texthere\\"
flaghacker #3
Posted 25 May 2014 - 07:45 PM
Escape the backslash with another backslash.
local text="/texthere\\"

Thanks!
Inksaver #4
Posted 25 May 2014 - 07:46 PM

local text = string.char(47).."texthere"..string.char(92)
Lignum #5
Posted 25 May 2014 - 07:48 PM

local text = string.char(47).."texthere"..string.char(92)
Well, that's adventurous ;)/>! The slash doesn't need to be escaped, only the backslash, but it's an acceptable solution.
Inksaver #6
Posted 25 May 2014 - 07:50 PM
Old habits die hard (VB6)!
flaghacker #7
Posted 25 May 2014 - 09:35 PM

local text = string.char(47).."texthere"..string.char(92)

Are those numbers the same as the ones you get from os.pullEvent("char")?
Dog #8
Posted 25 May 2014 - 09:37 PM

local text = string.char(47).."texthere"..string.char(92)

Are those numbers the same as the ones you get from os.pullEvent("char")?
"char" just returns strings, if I remember correctly. Do you, perchance, mean os.pullEvent("key") ?
Lignum #9
Posted 25 May 2014 - 09:45 PM
Are those numbers the same as the ones you get from os.pullEvent("char")?
If you were to wrap the result in a string.byte(), yes. If, as Dog said, you meant os.pullEvent("key"), no. The numbers returned by the key event are key codes.
Inksaver #10
Posted 25 May 2014 - 09:49 PM
The numbers are the decimal equivalent of the ASCII system.

I had to use them a lot with VB6

string.char(X) is the same as Chr$(X) in VB
"\" has the ASCII code of 97

The full list is here
theoriginalbit #11
Posted 26 May 2014 - 12:14 AM
no one is going to mention this?

local text = [[/text here\]]
flaghacker #12
Posted 26 May 2014 - 05:37 PM
no one is going to mention this?

local text = [[/text here\]]

That's definitely the most elegant solution!