Regular expressions - string substitution

N

Ne Scripter

Hello all,

A quick question if I have a string like so

000000100

and I want to remove the leading 0's to leave me with a string like so

100

What is the best way to do this? initially I was using
string.delete("0") but that obviously deletes every 0 and is not the
desired outcome. I then though string.gsub(/^0/, "") however this only
gets rid of the very first 0. Does anyone have any suggestions?

Thanks a lot
 
M

Michael Tomer

So long as you'll always be converting numbers, you can use to_i. That
will convert the string into an integer.
"000000100".to_i
=> 100

Keep in mind that it will truncate the number as soon as it reaches a
non-number. For instance:
"00012,345,678".to_i
=> 12

If you need to keep decimals then you can use to_f. For example:
"0001234.567".to_f
=> 1234.567
 
S

spiralofhope

string.gsub(/^0/, "")

Try something like this:

string.gsub(/^0*/, "")

This means: 0 or more occurrences of character "0", starting at the
beginning of the string.
 
J

James Edward Gray II

Hello all,

A quick question if I have a string like so

000000100

and I want to remove the leading 0's to leave me with a string like so

100

What is the best way to do this? initially I was using
string.delete("0") but that obviously deletes every 0 and is not the
desired outcome. I then though string.gsub(/^0/, "") however this only
gets rid of the very first 0. Does anyone have any suggestions?

"000000100".sub(/\A0+/, "")

\A means the beginning of the String and 0+ means one or more zeros.

Also note the use of sub() which only matches once over gsub() which
is used to repeat for all matches. There can only be one run of zeros
at the front of the String, so sub() is all we need.

Hope that helps.

James Edward Gray II
 
F

Fleck Jean-Julien

Hello,
What is the best way to do this? initially I was using
string.delete("0") but that obviously deletes every 0 and is not the
desired outcome. I then though string.gsub(/^0/, "") however this only
gets rid of the very first 0. Does anyone have any suggestions?

string.sub(/^0+/,'')

should do what you are looking for.


Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
R

Rob Biedenharn

Hello all,

A quick question if I have a string like so

000000100

and I want to remove the leading 0's to leave me with a string like so

100

What is the best way to do this? initially I was using
string.delete("0") but that obviously deletes every 0 and is not the
desired outcome. I then though string.gsub(/^0/, "") however this only
gets rid of the very first 0. Does anyone have any suggestions?

Thanks a lot
--


string.sub(/^0*/,'')

Or /^0+/ since both work if there are zeroes, but find nothing if the
first character is not a 0.

If your intention is to convert to a number, then one of the other
responses will work, but since you presented a string and asked to be
left with a string, I wanted to be sure you saw this simple tweak to
what you'd already discovered.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top