source: trunk/SRC/ToBeReviewed/STRING/strrepl.pro @ 171

Last change on this file since 171 was 157, checked in by navarro, 18 years ago

header improvements + xxx doc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1;-------------------------------------------------------------
2;+
3;
4; @file_comments
5; replace one (or more) character(s)/string(s) in a string
6;
7; @categories
8; String
9;
10; @param STR {in}{required}
11; the string to be changed
12;
13; @param RCHAR {in}{required}
14; replacement character/string
15;
16; @returns
17; another string
18;
19; @restrictions
20; Known shortcoming: if index is an array, it must contain all
21; valid elements (only the first entry is checked).
22;
23; @examples
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;
45; @history
46;        mgs, 02 Jun 1998: VERSION 1.00
47;        sebastien Masson (smlod\@ipsl.jussieu.fr)
48;
49; @version
50; $Id$
51;
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
65function strrepl,str,agument1,rchar
66;
67  compile_opt idl2, strictarrsubs
68;
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   
109end
Note: See TracBrowser for help on using the repository browser.