String.str_replace ????

Discussion in 'Ruby' started by Eko Budi Setiyo, Jan 25, 2005.

  1. Hi all

    Any body can help me with this str_replace problem.

    thanks

    ______________________________________

    class String
    def str_replace(str_in,str_out,string = self.to_s)
    result = String.new
    str_in_counter = 0
    string_pointer = 0
    str_in_counter = 0


    if str_in.length <= 1

    string.length.times{
    if string[string_pointer] == str_in
    result += str_out
    else
    result += string[string_pointer]
    end
    }

    else

    buffer = ''
    string.length.times{
    if string[string_pointer].to_s == str_in[str_in_counter].to_s
    buffer += string_array[string_pointer].dup
    if buffer.length == str_in_length
    result += str_out
    str_in_counter = -1
    buffer =''
    end
    else
    result += buffer + string[string_pointer]
    str_in_counter = -1
    buffer = ''
    end
    str_in_counter += 1
    string_pointer += 1
    }

    end

    result
    end



    require 'test/unit'

    class Test_string_replace < Test::Unit::TestCase

    def test_replace
    check = '03'
    source = 'aa0a3aa'
    assert_equal(check,source.str_replace('a',''))

    check = '03'
    source = 'aaaaaa0aaaa3aaaaaaaa'
    assert_equal(check,source.str_ireplace('aa',''))

    end

    end
    Eko Budi Setiyo, Jan 25, 2005
    #1
    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. Mladen Adamovic
    Replies:
    0
    Views:
    721
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,555
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    476
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,862
    John C. Bollinger
    Nov 19, 2004
  5. Anjanesh Lekshminarayanan

    PHP's str_replace ?

    Anjanesh Lekshminarayanan, Sep 10, 2008, in forum: Python
    Replies:
    7
    Views:
    359
    Grzegorz Staniak
    Sep 10, 2008
Loading...

Share This Page