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