1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; replace one (or more) character(s)/string(s) in a string |
---|
5 | ; --- OBSOLETE --- you should better use <pro>strsed</pro> |
---|
6 | ; |
---|
7 | ; @categories |
---|
8 | ; String |
---|
9 | ; |
---|
10 | ; @param STR {in}{required} |
---|
11 | ; the string to be changed |
---|
12 | ; |
---|
13 | ; @param ARG2 {in}{required} |
---|
14 | ; position of the character(s) to be replaced or a string to be changed in STR. |
---|
15 | ; |
---|
16 | ; @param RCHAR {in}{required} |
---|
17 | ; replacement character/string |
---|
18 | ; |
---|
19 | ; @returns |
---|
20 | ; another string |
---|
21 | ; |
---|
22 | ; @restrictions |
---|
23 | ; Known shortcoming: if index is an array, it must contain all |
---|
24 | ; valid elements (only the first entry is checked). |
---|
25 | ; |
---|
26 | ; @examples |
---|
27 | ; ; Convert one letter into upper case |
---|
28 | ; |
---|
29 | ; abc = 'abcdefghijklmnopqrstuvwxyz' |
---|
30 | ; print,strrepl(abc,strpos(abc,'m'),'M') |
---|
31 | ; |
---|
32 | ; ; prints "abcdefghijklMnopqrstuvwxyz" |
---|
33 | ; |
---|
34 | ; |
---|
35 | ; ; Use with strwhere function |
---|
36 | ; a = 'abcabcabc' |
---|
37 | ; print,strrepl(a,strwhere(a,'a'),'#') |
---|
38 | ; |
---|
39 | ; ; prints "#bc#bc#bc#bc#bc" |
---|
40 | ; |
---|
41 | ; IDL> print, strrepl(a,'bc','!eeee!') |
---|
42 | ; a!eeee!a!eeee!a!eeee! |
---|
43 | ; IDL> print, strrepl(a,'b','0000') |
---|
44 | ; a0000ca0000ca0000 |
---|
45 | ; IDL> print, strrepl(a,'toto','0000') |
---|
46 | ; abcabcabc |
---|
47 | ; |
---|
48 | ; @history |
---|
49 | ; mgs, 02 Jun 1998: VERSION 1.00 |
---|
50 | ; Copyright (C) 1998, Martin Schultz, Harvard University |
---|
51 | ; This software is provided as is without any warranty |
---|
52 | ; whatsoever. It may be freely used, copied or distributed |
---|
53 | ; for non-commercial purposes. This copyright notice must be |
---|
54 | ; kept with any copy of this software. If this software shall |
---|
55 | ; be used commercially or sold as part of a larger package, |
---|
56 | ; please contact the author to arrange payment. |
---|
57 | ; Bugs and comments should be directed to mgs\@io.harvard.edu |
---|
58 | ; with subject "IDL routine strrepl" |
---|
59 | ; |
---|
60 | ; sebastien Masson (smlod\@ipsl.jussieu.fr) |
---|
61 | ; |
---|
62 | ; @version |
---|
63 | ; $Id$ |
---|
64 | ; |
---|
65 | ;- |
---|
66 | FUNCTION strrepl,str,arg2,rchar |
---|
67 | ; |
---|
68 | compile_opt idl2, strictarrsubs, obsolete |
---|
69 | ; |
---|
70 | |
---|
71 | if (n_elements(str) eq 0) then return,'' |
---|
72 | |
---|
73 | ; convert strign and replace character to byte |
---|
74 | BStr = byte(str) |
---|
75 | new = byte(rchar) |
---|
76 | if size(arg2, /type) EQ 7 then begin |
---|
77 | old = byte(arg2) |
---|
78 | index = strpos(str, arg2) |
---|
79 | pos = index |
---|
80 | while strpos(str, arg2, pos+1) NE -1 do BEGIN |
---|
81 | pos = strpos(str, arg2, pos+1) |
---|
82 | index = [index, pos] |
---|
83 | ENDWHILE |
---|
84 | ; make sure index is in range |
---|
85 | if (index[0] lt 0 OR index[0] ge n_elements(BStr)) THEN return,Str |
---|
86 | ENDIF ELSE BEGIN |
---|
87 | index = arg2 |
---|
88 | if (index[0] lt 0 OR index[0] ge n_elements(BStr)) then return,Str |
---|
89 | old = BStr[index[0]] |
---|
90 | ENDELSE |
---|
91 | ; replace indexed characters in string |
---|
92 | nelenew = n_elements(new) |
---|
93 | neleold = n_elements(old) |
---|
94 | nindex = n_elements(index) |
---|
95 | if nelenew*neleold NE 1 then begin |
---|
96 | if index[0] EQ 0 then $ |
---|
97 | BStr = [NEW, BStr[index[0]+neleold: n_elements(BStr)-1] ] ELSE $ |
---|
98 | BStr = [BStr[0:index[0]-1], NEW, BStr[index[0]+neleold: n_elements(BStr)-1] ] |
---|
99 | if nindex EQ 1 then return,string(BStr) |
---|
100 | if nindex GT 2 then $ |
---|
101 | for i = 1, nindex-2 do $ |
---|
102 | BStr = [BStr[0:index[i]+i*(nelenew-neleold)-1], NEW $ |
---|
103 | , BStr[index[i]+i*(nelenew-neleold)+neleold: n_elements(BStr)-1] ] |
---|
104 | BStr = [BStr[0:index[n_elements(index)-1]+(nindex-1)*(nelenew-neleold)-1], NEW] |
---|
105 | |
---|
106 | ENDIF ELSE BStr[index] = NEW |
---|
107 | ; return result as string |
---|
108 | return,string(BStr) |
---|
109 | |
---|
110 | end |
---|