String.str_replace ????

E

Eko Budi Setiyo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top