2313
|
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. |
2288
|
19 |
2311
|
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 |
2312
|
25 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
26 ## Created: 11 November 1994 |
|
27 ## Adapted-By: jwe |
|
28 |
2288
|
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 s = toascii (s); |
|
50 x = toascii (x); |
|
51 y = toascii (y); |
|
52 |
|
53 l_x = length (x); |
|
54 tmp = s (1 : ind (1) - 1); |
|
55 t = [tmp, y]; |
|
56 for k = 1 : len - 1 |
|
57 tmp = s (ind (k) + l_x : ind (k+1) - 1); |
|
58 t = [t, tmp, y]; |
|
59 endfor |
|
60 tmp = s (ind(len) + l_x : length (s)); |
|
61 t = [t, tmp]; |
|
62 t = setstr (t); |
|
63 endif |
|
64 |
|
65 endfunction |