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

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

several bugfix + new strsed

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