# How to check if a string is a integer?

**URL:** <https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909>\
**Category:** Ruby\
**Created:** [June 4, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909 "2007-06-04T15:39:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/1 "2007-06-04T15:39:44Z")

</div>

How to check if a string is a integer ?

After seached in google, I can’t find any information. All ISINTEGER  
function is about Javascript.

Thank you.

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/2 "2007-06-04T15:46:45Z")

</div>

w wg wrote:

> How to check if a string is a integer ?
> 
> After seached in google, I can’t find any information. All ISINTEGER  
> function is about Javascript.
> 
> Thank you.

You can use Integer(), it raises if the String passed is not an integer.  
In case Integer() confuses you: that’s Kernel#Integer(), a method, not  
the class Integer.  
Also you can use a regex, e.g. str =~ /\A[±]?\d+\z/

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/3 "2007-06-04T15:49:47Z")

</div>

On 04/06/07, w wg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to check if a string is a integer ?

You can use either the String#to\_i or Kernel#Integer methods

irb(main):001:0\> “1234”.to\_i  
=\> 1234  
irb(main):002:0\> “123A”.to\_i  
=\> 123  
irb(main):003:0\> “A123”.to\_i  
=\> 0  
irb(main):004:0\> Integer(“A1234”)  
ArgumentError: invalid value for Integer: “A1234”  
from (irb):4:in `Integer' from (irb):4 irb(main):005:0> Integer("1234A") ArgumentError: invalid value for Integer: "1234A" from (irb):5:in `Integer’  
from (irb):5  
irb(main):006:0\> Integer(“1234”)  
=\> 1234  
irb(main):007:0\>

Farrel

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/4 "2007-06-04T15:56:16Z")

</div>

w wg wrote:

> How to check if a string is a integer ?
> 
> After seached in google, I can’t find any information. All ISINTEGER  
> function is about Javascript.
> 
> Thank you.

Call the is\_a? (or kind\_of?) method on the object:

123.is\_a? Integer  
=\> true  
‘David’.is\_a? Integer  
=\> false

David

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/5 "2007-06-04T16:02:48Z")

</div>

Thank you, it works.

How did you find the Integer method of class Kernel ?

I typed “ri Kernel”, but I can’t find any Kernel’s class method.

2007/6/4, Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/6 "2007-06-04T16:05:48Z")

</div>

On 04/06/07, David M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> w wg wrote:  
> 123.is\_a? Integer  
> =\> true  
> ‘David’.is\_a? Integer  
> =\> false

That won’t work

irb(main):001:0\> “123”.is\_a? Integer  
=\> false

Farrel

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/7 "2007-06-04T16:39:53Z")

</div>

Hi,

Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan R.:

> w wg wrote:
> 
> > How to check if a string is a integer ?
> 
> Also you can use a regex, e.g. str =~ /\A[±]?\d+\z/

In many cases it’s useful to test for hex representations as  
well. Something like:

class String  
def as\_uint  
case self  
when /\A0x[0-9a-f]+\z/i then hex  
when /\A0b[0-1]+\z/ then bin  
when /\A0[0-7]+\z/ then oct  
when /\A(?:0|[1-9]\d\*)\z/ then to\_i  
end  
end  
end

There should be a combination of Kernel#eval and $SAFE, too.

Bertram

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/8 "2007-06-04T18:00:11Z")

</div>

Bertram S. wrote:

> Hi,
> 
> Am Montag, 04. Jun 2007, 22:46:45 +0900 schrieb Stefan R.:
> 
> > w wg wrote:
> > 
> > > How to check if a string is a integer ?
> > 
> > Also you can use a regex, e.g. str =~ /\A[±]?\d+\z/
> 
> In many cases it’s useful to test for hex representations as  
> well. Something like:

In those cases I’d even more use Integer(). It accepts about all  
representations of Integers ruby itself understands. It is probably much  
faster than a case/when, you also will have the value converted to an  
integer afterwards, which you most probably want anyway.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/9 "2007-06-04T16:50:19Z")

</div>

Farrel L. wrote:

> On 04/06/07, David M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > w wg wrote:  
> > 123.is\_a? Integer  
> > =\> true  
> > ‘David’.is\_a? Integer  
> > =\> false
> 
> That won’t work
> 
> irb(main):001:0\> “123”.is\_a? Integer  
> =\> false
> 
> Farrel

Of course not. Duh! My apologies.

Perhaps I shouldn’t post before my first cup of tea.

David

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 5, 2007, 3:46am UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/10 "2007-06-05T03:46:34Z")

</div>

From: w wg [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] :

# I typed “ri Kernel”, but I can’t find any Kernel’s class method.

## C:\family\ruby\>qri integer --------------------------------------------------------- Kernel#Integer Integer(arg) =\> integer

```
 Converts _arg_ to a +Fixnum+ or +Bignum+. Numeric types are
 converted directly (with floating point numbers being truncated).
 If _arg_ is a +String+, leading radix indicators (+0+, +0b+, and
 +0x+) are honored. Others are converted using +to_int+ and +to_i+.
 This behavior is different from that of +String#to_i+.

    Integer(123.999) #=> 123
    Integer("0x1a") #=> 26
    Integer(Time.new) #=> 1049896590

```

same w qri kernel#integer

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [June 4, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/how-to-check-if-a-string-is-a-integer/105909/11 "2007-06-04T18:06:03Z")

</div>

Hi,

Am Dienstag, 05. Jun 2007, 01:00:10 +0900 schrieb Stefan R.:

> In those cases I’d even more use Integer(). It accepts about all  
> representations of Integers ruby itself understands.

Aah! I did not know that. Cool. Thanks.

Bertram
