home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / strings / strrep.m < prev    next >
Text File  |  1998-11-10  |  2KB  |  67 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. ## usage:  strrep (s, x, y)
  21. ##
  22. ## Replace all occurences of the substring x of the string s with the
  23. ## string y.
  24.  
  25. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  26. ## Created: 11 November 1994
  27. ## Adapted-By: jwe
  28.  
  29. function t = strrep (s, x, y)
  30.  
  31.   if (nargin <> 3)
  32.     usage ("strrep (s, x, y)");
  33.   endif
  34.  
  35.   if (! (isstr (s) && isstr (x) && isstr (y)))
  36.     error ("strrep: all arguments must be strings");
  37.   endif
  38.  
  39.   if (length (x) > length (s) || isempty (x))
  40.     t = s;
  41.     return;
  42.   endif
  43.  
  44.   ind = findstr (s, x, 0);
  45.   len = length (ind);
  46.   if (len == 0)
  47.     t = s;
  48.   else
  49.     save_empty_list_elements_ok = empty_list_elements_ok;
  50.     unwind_protect
  51.       empty_list_elements_ok = 1;
  52.       l_x = length (x);
  53.       tmp = s (1 : ind (1) - 1);
  54.       t = strcat (tmp, y);
  55.       for k = 1 : len - 1
  56.           tmp = s (ind (k) + l_x : ind (k+1) - 1);
  57.           t = strcat (t, tmp, y);
  58.       endfor
  59.       tmp = s (ind(len) + l_x : length (s));
  60.       t = [t, tmp];
  61.     unwind_protect_cleanup
  62.       empty_list_elements_ok = save_empty_list_elements_ok;
  63.     end_unwind_protect
  64.   endif
  65.  
  66. endfunction
  67.