I''m importing rows and want to edit names as I parse each row. Essentially I would like to do: "Mr. Mike Smith" -> "Mike Smith" when I use: row[0].delete "Mr" The string becomes "ike Smith" for some reason. I''ve tried decoding the whole regex thing with things like "/Mr/" but that''s not working either. Can anyone help? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
''Mr. Mike Smith''.replace(/mr\.\s+/i, ''''); // => ''Mike Smith'' - kangax On May 6, 11:46 am, Chris <tur...-7ZEPei9mWc08tMTU65lQ3Q@public.gmane.org> wrote:> I''m importing rows and want to edit names as I parse each row. > > Essentially I would like to do: > "Mr. Mike Smith" -> "Mike Smith" > > when I use: row[0].delete "Mr" > > The string becomes "ike Smith" for some reason. > > I''ve tried decoding the whole regex thing with things like "/Mr/" but > that''s not working either. > > Can anyone help?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, May 6, 2008 at 9:46 AM, Chris <turkey-7ZEPei9mWc08tMTU65lQ3Q@public.gmane.org> wrote:> > Essentially I would like to do: > "Mr. Mike Smith" -> "Mike Smith" > > when I use: row[0].delete "Mr"Since neither JS nor Prototype has a delete method for string objects, I assume you''re referring to Ruby''s String::delete method. That''s probably better suited for a Ruby or Rails list, (since this list is specifically geared towards Prototype) but I''ll see if I can help (I''m not a Ruby expert by any means).> The string becomes "ike Smith" for some reason.Here''s what delete does, according to the Ruby docs: "Returns a copy of str with all characters in the intersection of its arguments deleted." That means that when you pass, for example, "Mr" to delete, it''s going to delete /every/ "M" and /every/ "r" it finds in the string. As far as I can tell, in your case you should actually be getting ". ike Smith" back from delete (not "ike Smith"). If you pass "Mr." to delete, then every period character is going to be removed as well, giving you " ike Smith" (note the leading space). Passing "Mr. " (with a trailing space) gets you "ikeSmith". I''m sure you can tell that delete''s probably not the method you need ;) You could probably use sub, though, which seems to do closer to what you''re trying to accomplish. You need to pass the string you''re looking for and what you want to replace it with. For example: row[0].sub ''Mr. '', '''' => "Mike Smith" Note that you can also use regexes instead of string arguments, in which case the example Kangax provided will work perfectly. The Ruby docs for sub are at <http://www.ruby-doc.org/core/classes/String.html#M000831>. (You might want to check out gsub as well.) Hope that helps. :Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Kangax - Thanks very much... will try this out. On May 6, 12:55 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ''Mr. Mike Smith''.replace(/mr\.\s+/i, ''''); // => ''Mike Smith'' > > - kangax > > On May 6, 11:46 am, Chris <tur...-7ZEPei9mWc08tMTU65lQ3Q@public.gmane.org> wrote: > > > > > I''m importing rows and want to edit names as I parse each row. > > > Essentially I would like to do: > > "Mr. Mike Smith" -> "Mike Smith" > > > when I use: row[0].delete "Mr" > > > The string becomes "ike Smith" for some reason. > > > I''ve tried decoding the whole regex thing with things like "/Mr/" but > > that''s not working either. > > > Can anyone help?- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Dan - that was great... worked like a charm. Thanks! On May 6, 1:26 pm, "Dan Dorman" <dan.dor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, May 6, 2008 at 9:46 AM, Chris <tur...-7ZEPei9mWc08tMTU65lQ3Q@public.gmane.org> wrote: > > > Essentially I would like to do: > > "Mr. Mike Smith" -> "Mike Smith" > > > when I use: row[0].delete "Mr" > > Since neither JS nor Prototype has a delete method for string objects, > I assume you''re referring to Ruby''s String::delete method. That''s > probably better suited for a Ruby or Rails list, (since this list is > specifically geared towards Prototype) but I''ll see if I can help (I''m > not a Ruby expert by any means). > > > The string becomes "ike Smith" for some reason. > > Here''s what delete does, according to the Ruby docs: > > "Returns a copy of str with all characters in the intersection of its > arguments deleted." > > That means that when you pass, for example, "Mr" to delete, it''s going > to delete /every/ "M" and /every/ "r" it finds in the string. As far > as I can tell, in your case you should actually be getting ". ike > Smith" back from delete (not "ike Smith"). If you pass "Mr." to > delete, then every period character is going to be removed as well, > giving you " ike Smith" (note the leading space). Passing "Mr. " (with > a trailing space) gets you "ikeSmith". I''m sure you can tell that > delete''s probably not the method you need ;) > > You could probably use sub, though, which seems to do closer to what > you''re trying to accomplish. You need to pass the string you''re > looking for and what you want to replace it with. For example: > > row[0].sub ''Mr. '', '''' => "Mike Smith" > > Note that you can also use regexes instead of string arguments, in > which case the example Kangax provided will work perfectly. > > The Ruby docs for sub are at > <http://www.ruby-doc.org/core/classes/String.html#M000831>. (You might > want to check out gsub as well.) > > Hope that helps. > > :Dan--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, May 6, 2008 at 5:24 PM, Chris <turkey-7ZEPei9mWc08tMTU65lQ3Q@public.gmane.org> wrote:> > Dan - that was great... worked like a charm. Thanks!You''re welcome :) :Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---