home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / strings / strrep.m < prev    next >
Text File  |  1999-11-19  |  2KB  |  73 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} strrep (@var{s}, @var{x}, @var{y})
  22. ## Replaces all occurrences of the substring @var{x} of the string @var{s}
  23. ## with the string @var{y}.  For example,
  24. ## 
  25. ## @example
  26. ## strrep ("This is a test string", "is", "&%$")
  27. ##      @result{} "Th&%$ &%$ a test string"
  28. ## @end example
  29. ## @end deftypefn
  30.  
  31. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  32. ## Created: 11 November 1994
  33. ## Adapted-By: jwe
  34.  
  35. function t = strrep (s, x, y)
  36.  
  37.   if (nargin <> 3)
  38.     usage ("strrep (s, x, y)");
  39.   endif
  40.  
  41.   if (! (isstr (s) && isstr (x) && isstr (y)))
  42.     error ("strrep: all arguments must be strings");
  43.   endif
  44.  
  45.   if (length (x) > length (s) || isempty (x))
  46.     t = s;
  47.     return;
  48.   endif
  49.  
  50.   ind = findstr (s, x, 0);
  51.   len = length (ind);
  52.   if (len == 0)
  53.     t = s;
  54.   else
  55.     save_empty_list_elements_ok = empty_list_elements_ok;
  56.     unwind_protect
  57.       empty_list_elements_ok = 1;
  58.       l_x = length (x);
  59.       tmp = s (1 : ind (1) - 1);
  60.       t = strcat (tmp, y);
  61.       for k = 1 : len - 1
  62.           tmp = s (ind (k) + l_x : ind (k+1) - 1);
  63.           t = strcat (t, tmp, y);
  64.       endfor
  65.       tmp = s (ind(len) + l_x : length (s));
  66.       t = [t, tmp];
  67.     unwind_protect_cleanup
  68.       empty_list_elements_ok = save_empty_list_elements_ok;
  69.     end_unwind_protect
  70.   endif
  71.  
  72. endfunction
  73.