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

Last change on this file since 378 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 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;
28;  Convert one letter into upper case
29;
30;   IDL> abc = 'abcdefghijklmnopqrstuvwxyz'
31;   IDL> print,strrepl(abc,strpos(abc,'m'),'M')
32;   abcdefghijklMnopqrstuvwxyz
33;
34;
35;  Use with strwhere function
36;   IDL> a = 'abcabcabc'
37;   IDL> print,strrepl(a,strwhere(a,'a'),'#')
38;   #bc#bc#bc#bc#bc
39;
40;   IDL> print, strrepl(a,'bc','!eeee!')
41;       a!eeee!a!eeee!a!eeee!
42;   IDL> print, strrepl(a,'b','0000')
43;       a0000ca0000ca0000
44;   IDL> print, strrepl(a,'toto','0000')
45;       abcabcabc
46;
47; @history
48;        mgs, 02 Jun 1998: VERSION 1.00
49; Copyright (C) 1998, Martin Schultz, Harvard University
50; This software is provided as is without any warranty
51; whatsoever. It may be freely used, copied or distributed
52; for non-commercial purposes. This copyright notice must be
53; kept with any copy of this software. If this software shall
54; be used commercially or sold as part of a larger package,
55; please contact the author to arrange payment.
56; Bugs and comments should be directed to mgs\@io.harvard.edu
57; with subject "IDL routine strrepl"
58;
59;        sebastien Masson (smlod\@ipsl.jussieu.fr)
60;
61; @version
62; $Id$
63;
64;-
65FUNCTION strrepl,str,arg2,rchar
66;
67  compile_opt idl2, strictarrsubs, obsolete
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(arg2, /type) EQ 7 then begin
76      old = byte(arg2)
77      index = strpos(str, arg2)
78      pos = index
79      while strpos(str, arg2, pos+1) NE -1 do BEGIN
80         pos = strpos(str, arg2, 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 = arg2
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.