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
RevLine 
[2]1;+
2;
[231]3; @file_comments
[142]4; replace one (or more) character(s)/string(s) in a string
[231]5; --- OBSOLETE --- you should better use <pro>strsed</pro>
[2]6;
[142]7; @categories
[157]8; String
[2]9;
[142]10; @param STR {in}{required}
11; the string to be changed
[2]12;
[192]13; @param ARG2 {in}{required}
14; position of the character(s) to be replaced or a string to be changed in STR.
15;
[142]16; @param RCHAR {in}{required}
17; replacement character/string
[2]18;
[142]19; @returns
20; another string
[2]21;
[231]22; @restrictions
[142]23; Known shortcoming: if index is an array, it must contain all
24; valid elements (only the first entry is checked).
[2]25;
[142]26; @examples
[2]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;
[142]48; @history
[2]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.
[232]57; Bugs and comments should be directed to mgs\@io.harvard.edu
[2]58; with subject "IDL routine strrepl"
[231]59;
60;        sebastien Masson (smlod\@ipsl.jussieu.fr)
61;
62; @version
63; $Id$
64;
65;-
66;
[232]67FUNCTION strrepl,str,arg2,rchar
[114]68;
[192]69  compile_opt idl2, strictarrsubs, obsolete
[114]70;
[231]71
[2]72   if (n_elements(str) eq 0) then return,''
[231]73
[2]74                                ; convert strign and replace character to byte
75   BStr = byte(str)
76   new = byte(rchar)
[192]77   if size(arg2, /type) EQ 7 then begin
78      old = byte(arg2)
79      index = strpos(str, arg2)
[2]80      pos = index
[192]81      while strpos(str, arg2, pos+1) NE -1 do BEGIN
82         pos = strpos(str, arg2, pos+1)
[2]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
[192]88      index = arg2
[2]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)
[231]110
[2]111end
Note: See TracBrowser for help on using the repository browser.