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

  1. ## Copyright (C) 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:  m = split (s, t)
  21. ##
  22. ## Divides the string S into pieces separated by T, and stores the
  23. ## pieces as the rows of M (padded with blanks to form a valid
  24. ## matrix).
  25.  
  26. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  27. ## Adapted-By: jwe
  28.  
  29. function m = split (s, t)
  30.  
  31.   if (nargin != 2)
  32.     usage ("split (s, t)");
  33.   endif
  34.  
  35.   if (isstr (s) && isstr (t))
  36.  
  37.   l_s = length (s);
  38.   l_t = length (t);
  39.  
  40.   if (l_s < l_t)
  41.     error ("split: s must not be shorter than t");
  42.   endif
  43.  
  44.   if (l_t == 0)
  45.     ind = 1 : (l_s + 1);
  46.   else
  47.     ind = findstr (s, t, 0);
  48.     if (length (ind) == 0)
  49.       m = s;
  50.       return;
  51.     endif
  52.     ind = [1 - l_t, ind, l_s + 1];
  53.   endif
  54.  
  55.   cmd = "";
  56.  
  57.   limit = length (ind) - 1;
  58.  
  59.   for k = 1 : limit
  60.  
  61.     range = (ind (k) + l_t) : ind (k + 1) - 1;
  62.  
  63.     if (k != limit)
  64.       cmd = sprintf ("%s\"%s\", ", cmd, undo_string_escapes (s (range)));
  65.     else
  66.       cmd = sprintf ("%s\"%s\"", cmd, undo_string_escapes (s (range)));
  67.     endif
  68.  
  69.   endfor
  70.  
  71.   m = eval (sprintf ("str2mat (%s);", cmd));
  72.  
  73.  
  74.   else
  75.     error ("split:  both s and t must be strings");
  76.   endif
  77.  
  78. endfunction
  79.