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

Last change on this file since 412 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
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;
[371]28;  Convert one letter into upper case
[2]29;
[371]30;   IDL> abc = 'abcdefghijklmnopqrstuvwxyz'
31;   IDL> print,strrepl(abc,strpos(abc,'m'),'M')
32;   abcdefghijklMnopqrstuvwxyz
[2]33;
34;
[371]35;  Use with strwhere function
36;   IDL> a = 'abcabcabc'
37;   IDL> print,strrepl(a,strwhere(a,'a'),'#')
38;   #bc#bc#bc#bc#bc
[2]39;
[371]40;   IDL> print, strrepl(a,'bc','!eeee!')
[2]41;       a!eeee!a!eeee!a!eeee!
[371]42;   IDL> print, strrepl(a,'b','0000')
[2]43;       a0000ca0000ca0000
[371]44;   IDL> print, strrepl(a,'toto','0000')
[2]45;       abcabcabc
46;
[142]47; @history
[2]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.
[232]56; Bugs and comments should be directed to mgs\@io.harvard.edu
[2]57; with subject "IDL routine strrepl"
[231]58;
59;        sebastien Masson (smlod\@ipsl.jussieu.fr)
60;
61; @version
62; $Id$
63;
64;-
[232]65FUNCTION strrepl,str,arg2,rchar
[114]66;
[192]67  compile_opt idl2, strictarrsubs, obsolete
[114]68;
[231]69
[2]70   if (n_elements(str) eq 0) then return,''
[231]71
[2]72                                ; convert strign and replace character to byte
73   BStr = byte(str)
74   new = byte(rchar)
[192]75   if size(arg2, /type) EQ 7 then begin
76      old = byte(arg2)
77      index = strpos(str, arg2)
[2]78      pos = index
[192]79      while strpos(str, arg2, pos+1) NE -1 do BEGIN
80         pos = strpos(str, arg2, pos+1)
[2]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
[192]86      index = arg2
[2]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)
[231]108
[2]109end
Note: See TracBrowser for help on using the repository browser.