Jump to content




string.sub question


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

#1 Varscott11

  • Members
  • 23 posts

Posted 26 June 2016 - 03:58 PM

So I have a program that needs to take a string received through rednet, and determine what the last few letters are.
I have set up a string.sub to handle this that looks like this:

id, message = rednet.receive()
messagesub = string.sub(message, pos1, pos2)

I've tried:

local messagelength = #message - 4 --subtract for from the total length
message = string.sub(message, messagelength)

However, its keeps giving me an error, "attempt to get length of nil"

what I was wondering is if there is a way to get the last 4 letters of the string. Any help is appreciated!

#2 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 26 June 2016 - 04:06 PM

string.sub(message, -4) --#Last 4 characters in the string

Edited by TYKUHN2, 26 June 2016 - 04:06 PM.


#3 Varscott11

  • Members
  • 23 posts

Posted 26 June 2016 - 04:09 PM

Thanks for the reply! trying it now

Why thank you good sir. It works!

#4 Exerro

  • Members
  • 801 posts

Posted 26 June 2016 - 04:39 PM

Or even...

message:sub( -4 )

Your "attempt to get length of nil" error would suggest that 'message' was nil, however, so when you tried "#message", it errored.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users