2303
|
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 |
2288
|
25 function t = strrep (s, x, y) |
|
26 |
2311
|
27 ## Written by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> on 1994/10/11 |
2303
|
28 ## Updated by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> on 1996/05/25 |
2288
|
29 |
|
30 if (nargin <> 3) |
|
31 usage ("strrep (s, x, y)"); |
|
32 endif |
|
33 |
|
34 if (! (isstr (s) && isstr (x) && isstr (y))) |
|
35 error ("strrep: all arguments must be strings"); |
|
36 endif |
|
37 |
|
38 if (length (x) > length (s) || isempty (x)) |
|
39 t = s; |
|
40 return; |
|
41 endif |
|
42 |
|
43 ind = findstr (s, x, 0); |
|
44 len = length (ind); |
|
45 if (len == 0) |
|
46 t = s; |
|
47 else |
|
48 s = toascii (s); |
|
49 x = toascii (x); |
|
50 y = toascii (y); |
|
51 |
|
52 l_x = length (x); |
|
53 tmp = s (1 : ind (1) - 1); |
|
54 t = [tmp, y]; |
|
55 for k = 1 : len - 1 |
|
56 tmp = s (ind (k) + l_x : ind (k+1) - 1); |
|
57 t = [t, tmp, y]; |
|
58 endfor |
|
59 tmp = s (ind(len) + l_x : length (s)); |
|
60 t = [t, tmp]; |
|
61 t = setstr (t); |
|
62 endif |
|
63 |
|
64 endfunction |