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

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

modification of some headers (+some corrections) to prepare usage of the new idldoc

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