source: trunk/ToBeReviewed/STRING/strrepl.pro @ 18

Last change on this file since 18 was 18, checked in by pinsard, 18 years ago

upgrade of STRING according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/ : files

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
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
77function strrepl,str,agument1,rchar
78   
79   if (n_elements(str) eq 0) then return,''
80   
81                                ; convert strign and replace character to byte
82   BStr = byte(str)
83   new = byte(rchar)
84   if size(agument1, /type) EQ 7 then begin
85      old = byte(agument1)
86      index = strpos(str, agument1)
87      pos = index
88      while strpos(str, agument1, pos+1) NE -1 do BEGIN
89         pos = strpos(str, agument1, pos+1)
90         index = [index, pos]
91      ENDWHILE
92; make sure index is in range
93      if (index[0] lt 0 OR index[0] ge n_elements(BStr)) THEN return,Str
94   ENDIF ELSE BEGIN
95      index = agument1
96      if (index[0] lt 0 OR index[0] ge n_elements(BStr)) then return,Str
97      old = BStr[index[0]]
98   ENDELSE
99                                ; replace indexed characters in string
100   nelenew = n_elements(new)
101   neleold = n_elements(old)
102   nindex = n_elements(index)
103   if nelenew*neleold NE 1 then begin
104      if index[0] EQ 0 then $
105       BStr = [NEW,  BStr[index[0]+neleold: n_elements(BStr)-1] ] ELSE $
106       BStr = [BStr[0:index[0]-1], NEW,  BStr[index[0]+neleold: n_elements(BStr)-1] ]
107      if nindex EQ 1 then return,string(BStr)
108      if nindex GT 2 then $
109       for i = 1, nindex-2 do $
110       BStr = [BStr[0:index[i]+i*(nelenew-neleold)-1], NEW $
111               , BStr[index[i]+i*(nelenew-neleold)+neleold: n_elements(BStr)-1] ]
112      BStr = [BStr[0:index[n_elements(index)-1]+(nindex-1)*(nelenew-neleold)-1], NEW]
113
114   ENDIF ELSE BStr[index] = NEW
115                                ; return result as string
116   return,string(BStr)
117   
118end
Note: See TracBrowser for help on using the repository browser.