octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another implementation for strrep


From: Paul Kienzle
Subject: Re: Another implementation for strrep
Date: Fri, 9 Mar 2007 20:34:39 -0500


On Mar 9, 2007, at 6:16 PM, Thomas Treichl wrote:

Hi,

I did some tests with the strrep function and I think that I was able to implement a very short and very fast code that does depend very little on string sizes and the number of strings that have to be replaced:

...

Code file attached.
Bye, Thomas
function t = _strrep_ (s, x, y)

  if (nargin != 3), print_usage (); endif

  if (! (ischar (s) && ischar (x) && ischar (y)))
    error ("strrep: all arguments must be strings");
  endif

  [a, b] = regexp (s, x);
  b = [1, b+1]; a = [a-1, (length (s))];
  t = s(b(1):a(1));
  for cnt = 2:length (a)
    t = [t, y, s(b(cnt):a(cnt))];
  end

endfunction


Use regexprep and you get rid of the loop as well.

        - Paul



reply via email to

[Prev in Thread] Current Thread [Next in Thread]