2288
|
1 # Copyright (C) 1995, 1996 Kurt Hornik |
|
2 # |
|
3 # This program is free software; you can redistribute it and/or modify |
|
4 # it under the terms of the GNU General Public License as published by |
|
5 # the Free Software Foundation; either version 2, or (at your option) |
|
6 # any later version. |
|
7 # |
|
8 # This program is distributed in the hope that it will be useful, but |
|
9 # WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 # General Public License for more details. |
|
12 # |
|
13 # You should have received a copy of the GNU General Public License |
|
14 # along with this file. If not, write to the Free Software Foundation, |
|
15 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
|
17 function t = strrep (s, x, y) |
|
18 |
|
19 # usage: strrep (s, x, y) |
|
20 # |
|
21 # Replace all occurences of the substring x of the string s with the |
|
22 # string y. |
|
23 |
|
24 # Written by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> on 1994/10/11 |
|
25 # Updated by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> on 1996/05/25 |
|
26 |
|
27 if (nargin <> 3) |
|
28 usage ("strrep (s, x, y)"); |
|
29 endif |
|
30 |
|
31 if (! (isstr (s) && isstr (x) && isstr (y))) |
|
32 error ("strrep: all arguments must be strings"); |
|
33 endif |
|
34 |
|
35 if (length (x) > length (s) || isempty (x)) |
|
36 t = s; |
|
37 return; |
|
38 endif |
|
39 |
|
40 ind = findstr (s, x, 0); |
|
41 len = length (ind); |
|
42 if (len == 0) |
|
43 t = s; |
|
44 else |
|
45 s = toascii (s); |
|
46 x = toascii (x); |
|
47 y = toascii (y); |
|
48 |
|
49 l_x = length (x); |
|
50 tmp = s (1 : ind (1) - 1); |
|
51 t = [tmp, y]; |
|
52 for k = 1 : len - 1 |
|
53 tmp = s (ind (k) + l_x : ind (k+1) - 1); |
|
54 t = [t, tmp, y]; |
|
55 endfor |
|
56 tmp = s (ind(len) + l_x : length (s)); |
|
57 t = [t, tmp]; |
|
58 t = setstr (t); |
|
59 endif |
|
60 |
|
61 endfunction |