From: Skeets Date: 2006-07-31T07:15:06+09:00 Subject: Re: Howto Delete 3 Leftmost Characters Esteban Manchado Vel�zquez wrote: > On Mon, Jul 31, 2006 at 06:30:13AM +0900, Skeets wrote: > > i'm sure this is easy, but i've gone through Pickaxe's string methods, > > searched the web and searched the groups, and i can't figure out how to > > do this in Ruby. > > > > i grep a file and it returns the following string: > > > > #ip 127.0.0.1 > > > > i now want to get rid of "#ip" so i can then strip the remaining string > > to get rid of spaces. > > > > however, i can't find out how to delete the 3 leftmost characters - in > > this case "#ip". > > "#ip 127.0.0.1"[3..-1] # => " 127.0.0.1" > "#ip 127.0.0.1"[4..-1] # => "127.0.0.1" > > But you're probably better off using regular expressions instead of fixed > indices: > > "#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1" > > That is, "remove, from the beginning of the line, '#ip' followed by one or > more space characters (be them spaces, tabs or whatever)". If you don't know > regular expressions, _and_ you're into text processing, I recommend you to go > and read some book about regular expressions and practice a little (under > Linux there are a couple of handy utilities for that; I'm sure there must be > also for other platforms). > > Regards, > > -- > Esteban Manchado Vel�zquez - http://www.foton.es Esteban, and all - thank you. this did the trick... if File.exist?( 'current_ip.txt' ) f = File.open('current_ip.txt').grep(/#ip/) f = f[0].sub(/^#ip\s+/, '') end puts f the File.open grep line returned an array. i had to sort that out first. i know this file will always have only one instance of #ip - is there any way to force grep to return as a variable instead of an array? also, i believe the regex Esteban gave will get rid of all white spaces - both to the left and right of the ip address. thanks to everyone for the help.