Jump to content

Question in string.find


#Just_Me

Recommended Posts

You mean this?

function convertNumber ( number )   
 local formatted = number   
 while true do       
  formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')     
  if ( k==0 ) then       
   break    
  end   
 end   
 return formatted 
end 

Example:

local money = getPlayerMoney(source) 
guiSetText(label, "Money: "..convertNumber(money).."") 

result: xx,xxx,xxx

Link to comment

http://www.lua.org/pil/20.2.html

As we can see, we can't just use string.find(var, ".") because .find is meant to find patterns in strings and not strings within strings. That's why there is "%d", "%w" etc. And, "." matches all characters according to the link above.

Since we're looking for a pattern, we need to use the pattern operator before what we want to find (%). So string.find(var, "%.") would work. From this we can also see string.find has an argument on the end of it called plain. That matches a plain character without any pattern operators (so I can search for just "w" instead of "%w" which matches alphanumeric characters. So, you can do string.find(var, ".", 1, true). 1 represents the string index to begin at.

tl;dr

var = "100.000.000" 
string.find(var, "%.") 
-- or 
string.find(var, ".", 1, true) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...