2313
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
2325
|
2 ## |
2313
|
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 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} strrep (@var{s}, @var{x}, @var{y}) |
|
22 ## Replaces all occurrences of the substring @var{x} of the string @var{s} |
|
23 ## with the string @var{y}. For example, |
3426
|
24 ## |
3361
|
25 ## @example |
|
26 ## strrep ("This is a test string", "is", "&%$") |
|
27 ## @result{} "Th&%$ &%$ a test string" |
|
28 ## @end example |
|
29 ## @end deftypefn |
2311
|
30 |
2312
|
31 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
32 ## Created: 11 November 1994 |
|
33 ## Adapted-By: jwe |
|
34 |
2288
|
35 function t = strrep (s, x, y) |
2325
|
36 |
2288
|
37 if (nargin <> 3) |
|
38 usage ("strrep (s, x, y)"); |
|
39 endif |
2325
|
40 |
2288
|
41 if (! (isstr (s) && isstr (x) && isstr (y))) |
|
42 error ("strrep: all arguments must be strings"); |
|
43 endif |
2325
|
44 |
2288
|
45 if (length (x) > length (s) || isempty (x)) |
|
46 t = s; |
|
47 return; |
|
48 endif |
2325
|
49 |
2288
|
50 ind = findstr (s, x, 0); |
|
51 len = length (ind); |
|
52 if (len == 0) |
|
53 t = s; |
|
54 else |
3180
|
55 save_empty_list_elements_ok = empty_list_elements_ok; |
|
56 unwind_protect |
|
57 empty_list_elements_ok = 1; |
|
58 l_x = length (x); |
|
59 tmp = s (1 : ind (1) - 1); |
|
60 t = strcat (tmp, y); |
|
61 for k = 1 : len - 1 |
3426
|
62 tmp = s (ind (k) + l_x : ind (k+1) - 1); |
|
63 t = strcat (t, tmp, y); |
3180
|
64 endfor |
|
65 tmp = s (ind(len) + l_x : length (s)); |
|
66 t = [t, tmp]; |
|
67 unwind_protect_cleanup |
|
68 empty_list_elements_ok = save_empty_list_elements_ok; |
|
69 end_unwind_protect |
2288
|
70 endif |
2325
|
71 |
2288
|
72 endfunction |