source: trunk/SRC/Obsolete/strrepl.pro @ 232

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

improvements/corrections of some *.pro headers

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