From: jbw Date: 2010-03-30T12:06:47+09:00 Subject: Re: return number of spaces at the beginning of a line --00032555b95a5f43aa0482fbeb73 Content-Type: text/plain; charset=UTF-8 On Tue, Mar 30, 2010 at 3:41 AM, Jesse B. wrote: > How would I find the number of spaces at the beginning of a line before > the occurrance of the first non-space character? > > Would the best method be to use a regular expression that covers all > non-space characters and get the index of the first occurrance of that? > -- > Posted via http://www.ruby-forum.com/. > > You could use regex you could also, if the data is a string or an array, split them into a char array e.g. : s = " hello" s = s.chars.to_a => [" ", " ", " ", " ", "h", "e", "l", "l", "o"] s.each do | c | if( c == " " ) i = i+1 puts i end end and that'll count the number of spaces. -- jbw --00032555b95a5f43aa0482fbeb73--