|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey,
I have an IP address and I need to manipulate the final octet in a few different ways. For example, I have: Code:
str = 10.100.100.0 And I want to end up with: Code:
newStr1 = 10.100.100. Code:
newStr2 = 10.100.100.1 Now the IP address length may vary so I can't spilt up on string length, I need to split up after the 3rd occurence of '.' . In PHP I'd use substr_replace but I can't find a Ruby equivilant. Any ideas? Thanks! |
|
#2
|
|||
|
|||
|
Code:
irb(main):001:0> s = "10.1.1.1" => "10.1.1.1" irb(main):002:0> a = s.split '.' => ["10", "1", "1", "1"] irb(main):003:0> a => ["10", "1", "1", "1"] irb(main):004:0> a[3] = "200" => "200" irb(main):005:0> a.join '.' => "10.1.1.200" Or you can use ruby's 'intelligence' to your advantage: Code:
irb(main):001:0> s = "10.1.1.1" => "10.1.1.1" irb(main):002:0> s.next => "10.1.1.2"
__________________
Some people have 20 years of experience. Some have 1 year of experience 20 times. My personal site: Basic geek randomness |
|
#3
|
|||
|
|||
|
Split and Join work a treat! Thanks.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Searching and replacing in a string based character occurence |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|