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