Jump to content

getting a whole number instead of just a character


Karoffe

Recommended Posts

Well, I want to string.gsub a whole number insead of a character in a number

Example:

local anyString = "This whole number: 123456 , and this just a character in a number: 5"

so is there a way to string.gsub 123456 instead of 1 alone and 2 alone, etc....

Link to comment

You can do this by using regular expressions(Google it).

I'm on my phone at the moment so I can't give you and examples. But there should be plenty of related threads if you try and search.

What I can say is that %d stands for digit. So if there is a number, it will be deleted. You can then mix this with + to get all the numbers.

string.gsub(stringVariable, "%d+", "")

Link to comment

Well, I knew that but wanted to use it with string.match so I extract the part I want and add \n before the number

What if I don't want to change the value in string.gsub I want the number to stay as it is but adding an \n before the number, how can that be done?

Link to comment
That needs a bit more expertise.

Tho this piece should work

local str = "Lucky number 2569!" 
local e1,e2 = string.find(str,"%d+") 
str = string.sub(str,0,e1-1)..'/n'..string.sub(str,e1,string.len(str)) 

> "Lucky number /n2569!"

Yes that would work for just 1 whole number but it won't work if there is more..

so if the str was "Lucky numbers 2569, 3662, 5 !"

the output is going to be like this

> "Lucky numbers /n2569, 3662, 5 !"

I want the output to be like this

> "Lucky numbers /n2569, /n3662, /n5 !"

...

That's why I wanted to use string.match so I use (repeat-until) with inserting every extracted part to a table until there is no more numbers in the string and then draw every row in the table in the right position...

local anything = "This whole number: 123456" 
local number = tonumber(anything:match("%d+")) 
print(number) 

Output:

123456

Yes that would work if the string was like that only, but like I stated above it won't work with numerous "whole" numbers...

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...