Eko Budi Setiyo
1/25/2005 5:03:00 AM
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