I am trying to get at the first character in a string and when I say @string[0] I get an integer value, but when I say @string[0..0] I get the character. Why is that? Thanks, -S -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Strings are not treated the same as arrays. A single number in the brackets gives you the ASCII value of the character at that position. If you want the first character, I''d use @string[0,1] which gives you a substring starting at the 0th position, with a length of 1. --- Chris On Nov 16, 2007 5:01 PM, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I am trying to get at the first character in a string and when I say > @string[0] I get an integer value, but when I say @string[0..0] I get > the character. Why is that? Thanks, > > -S > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 16, 2007, at 5:22 PM, Chris Evans wrote:> Strings are not treated the same as arrays. A single number in the > brackets gives you the ASCII value of the character at that position. > If you want the first character, I''d use @string[0,1] which gives you > a substring starting at the 0th position, with a length of 1. > > --- > Chris > > On Nov 16, 2007 5:01 PM, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > wrote: >> >> I am trying to get at the first character in a string and when I say >> @string[0] I get an integer value, but when I say @string[0..0] I get >> the character. Why is that? Thanks, >> >> -SBUT, this will change in Ruby 2.0 so that "abc"[0] will be "a" rather than 97 It seems to be true that "something"[n] is the same value as "something"[n..n][0] for all n -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---