• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua string manipulating

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
Would you like to know how I make a function get my mount's name from an item description?

TFS 1.X

1625180001136.png

Mount: Cavalinho by [ADM] Veteranos


but this name "Cavalinho" is dynamic or it can change always, is there a possibility?

I would only need to return the name "Cavalinho"
 
Last edited:
Solution
1. Cut 'Mount: '
2. Split string using pattern ' by '. Players must not be able to use lowercase ' by ' as part of their name (default in acc. makers).
Code:
-- 'string:split' from http://lua-users.org/wiki/SplitJoin
function string:split( inSplitPattern, outResults )
   if not outResults then
      outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
      table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
      theStart = theSplitEnd + 1
      theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end


--...
look for an item that has a description, copy and paste the code and add your own desc..
I think I expressed myself wrong.

What I would like is to get the description of the item, but only the part written "Cavalinho", and the name "Cavalinho" is dynamic, that is, depending on the mount, the name in the description would change and I would have to get only the name .
 
1. Cut 'Mount: '
2. Split string using pattern ' by '. Players must not be able to use lowercase ' by ' as part of their name (default in acc. makers).
Code:
-- 'string:split' from http://lua-users.org/wiki/SplitJoin
function string:split( inSplitPattern, outResults )
   if not outResults then
      outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
      table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
      theStart = theSplitEnd + 1
      theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end


-- local description = item:getDescription()
local description = 'Mount: Cavalinho by [ADM] Veteranos'
print(description)
local noMountPart = string.sub(description, 8)
print(noMountPart)
local descriptionParts = noMountPart:split(' by ')
local playerName1 = descriptionParts[1]
local playerName2 = descriptionParts[2]
print('player one:', playerName1)
print('player two:', playerName2)
Tested on Lua: demo (https://www.lua.org/cgi-bin/demo)
 
Solution
Back
Top