Working with strings...

Discussion in 'Ruby' started by Tom Rathbone, Feb 3, 2005.

  1. Tom Rathbone

    Tom Rathbone Guest

    Hi,

    Is there a function that would work over the characters in String in
    the way that each works for an array? I want to process the string
    character by character and thought that #each would do this,
    apparently not. Even better would be a collect style method.
    Something like this.

    "some string".collect do |x|
    x + '|'
    end

    >> "s|o|m|e| |s|t|r|i|n|g|"


    #collect_char or #each_char would be great.

    Perhaps not the best example there, basically i intend to interpret a
    string of character commands from a string i.e. something like
    Brainf**k.

    Thanks,

    Tom.
    Tom Rathbone, Feb 3, 2005
    #1
    1. Advertising

  2. Try this:

    result = ''
    "some string".each_byte { |b| result << b << '|' }
    puts result

    --
    Szy
    Szymon Drejewicz, Feb 3, 2005
    #2
    1. Advertising

  3. Tom Rathbone

    Sea&Gull Guest

    Tom Rathbone wrote:
    > Hi,
    >
    > Is there a function that would work over the characters in String in
    > the way that each works for an array? I want to process the string
    > character by character and thought that #each would do this,
    > apparently not. Even better would be a collect style method.
    > Something like this.
    >
    > "some string".collect do |x|
    > x + '|'
    > end
    >
    >
    >>>"s|o|m|e| |s|t|r|i|n|g|"

    >
    >
    > #collect_char or #each_char would be great.


    "some string".split('').collect {|i| print i, '|'}


    >
    > Perhaps not the best example there, basically i intend to interpret a
    > string of character commands from a string i.e. something like
    > Brainf**k.
    >
    > Thanks,
    >
    > Tom.
    >
    >


    --
    s&g
    Sea&Gull, Feb 3, 2005
    #3
  4. Tom Rathbone wrote:

    > Is there a function that would work over the characters in String in
    > the way that each works for an array? I want to process the string
    > character by character and thought that #each would do this,
    > apparently not. Even better would be a collect style method.
    > Something like this.
    >
    > "some string".collect do |x|
    > x + '|'
    > end
    >
    >
    >>>"s|o|m|e| |s|t|r|i|n|g|"


    Generally you can do "some string".scan(/./).map { |x| x + "|" }

    But note that will construct an intermediate array which is a little
    inefficient -- if you have really big strings (a few hundred kilobyte)
    you're probably better off doing it like this instead:

    result = ""; "some string".scan(/./) { |x| result << x << "|" }; result
    Florian Gross, Feb 3, 2005
    #4
  5. Or:

    string.gsub!(/./) { |match| match += '|' }

    --
    Szy
    Szymon Drejewicz, Feb 3, 2005
    #5
  6. Tom Rathbone

    Wybo Dekker Guest

    On Thu, 3 Feb 2005, Szymon Drejewicz wrote:

    > Or:
    >
    > string.gsub!(/./) { |match| match += '|' }


    or:

    string.gsub!(/./,'\&|')


    --
    Wybo
    Wybo Dekker, Feb 3, 2005
    #6
  7. > string.gsub!(/./,'\&|')


    cool :) but it could be even shorter:

    string.gsub! //,'\0|'

    :)))

    (one leading '|' in output more)

    --
    Szy
    Szymon Drejewicz, Feb 3, 2005
    #7
  8. > (one leading '|' in output more)

    Better to rename the var >:)

    s.gsub!(/./,'\&|')
    Douglas Livingstone, Feb 3, 2005
    #8
  9. On Thu, 3 Feb 2005, Szymon Drejewicz wrote:

    > string.gsub! //,'\0|'
    >
    > :)))
    >
    > (one leading '|' in output more)
    >


    string.gsub! //,'|'


    Hugh
    Hugh Sasse Staff Elec Eng, Feb 3, 2005
    #9
  10. In Message-Id: <>
    Tom Rathbone <> writes:

    > "some string".collect do |x|
    > x + '|'
    > end
    >
    > >> "s|o|m|e| |s|t|r|i|n|g|"


    require "enumerator"

    p "some string".enum_for:)each_byte).inject("") {|s, c| s << c.chr << "|"}
    #=> "s|o|m|e| |s|t|r|i|n|g|"


    --
    February 4, 2005
    Speech is silver, silence is golden.
    YANAGAWA Kazuhisa, Feb 3, 2005
    #10
  11. Tom Rathbone wrote:
    > Hi,
    >
    > Is there a function that would work over the characters in String in
    > the way that each works for an array? I want to process the string
    > character by character and thought that #each would do this,
    > apparently not. Even better would be a collect style method.
    > Something like this.
    >
    > "some string".collect do |x|
    > x + '|'
    > end
    >
    > >> "s|o|m|e| |s|t|r|i|n|g|"

    >
    > #collect_char or #each_char would be great.
    >
    > Perhaps not the best example there, basically i intend to interpret a
    > string of character commands from a string i.e. something like
    > Brainf**k.
    >
    > Thanks,
    >
    > Tom.


    s = "some string"

    s.split('').join('|') + '|'
    # or
    s.unpack("A"*s.length).join('|') + '|'

    Regards,

    Dan
    Daniel Berger, Feb 3, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    694
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    366
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    470
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    557
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    732
    Malcolm
    Jun 24, 2006
Loading...

Share This Page