Jump to content




/ in strings


  • You cannot reply to this topic
11 replies to this topic

#1 flaghacker

  • Members
  • 655 posts

Posted 25 May 2014 - 06: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 by flaghacker, 25 May 2014 - 06:31 PM.


#2 Lignum

  • Members
  • 558 posts

Posted 25 May 2014 - 06:38 PM

Escape the backslash with another backslash.
local text="/texthere\\"


#3 flaghacker

  • Members
  • 655 posts

Posted 25 May 2014 - 06:45 PM

View PostLignum, on 25 May 2014 - 06:38 PM, said:

Escape the backslash with another backslash.
local text="/texthere\\"

Thanks!

#4 Inksaver

  • Members
  • 61 posts

Posted 25 May 2014 - 06:46 PM

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


#5 Lignum

  • Members
  • 558 posts

Posted 25 May 2014 - 06:48 PM

View PostInksaver, on 25 May 2014 - 06:46 PM, said:

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.

#6 Inksaver

  • Members
  • 61 posts

Posted 25 May 2014 - 06:50 PM

Old habits die hard (VB6)!

#7 flaghacker

  • Members
  • 655 posts

Posted 25 May 2014 - 08:35 PM

View PostInksaver, on 25 May 2014 - 06:46 PM, said:

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

Are those numbers the same as the ones you get from os.pullEvent("char")?

#8 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 25 May 2014 - 08:37 PM

View Postflaghacker, on 25 May 2014 - 08:35 PM, said:

View PostInksaver, on 25 May 2014 - 06:46 PM, said:

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") ?

#9 Lignum

  • Members
  • 558 posts

Posted 25 May 2014 - 08:45 PM

View Postflaghacker, on 25 May 2014 - 08:35 PM, said:

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.

#10 Inksaver

  • Members
  • 61 posts

Posted 25 May 2014 - 08: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

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 May 2014 - 11:14 PM

no one is going to mention this?
local text = [[/text here\]]


#12 flaghacker

  • Members
  • 655 posts

Posted 26 May 2014 - 04:37 PM

View Posttheoriginalbit, on 25 May 2014 - 11:14 PM, said:

no one is going to mention this?
local text = [[/text here\]]

That's definitely the most elegant solution!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users