source: trunk/SRC/ToBeReviewed/STRING/strsed.pro @ 371

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

improvements of headers (alignments of IDL prompt in examples)

  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1;+
2;
3; @file_comments
4; replace one (or more) character(s)/string(s) in a string array
5; "modern" version of the obsolete strrepl
6;
7; @categories
8; String
9;
10; @param STR {in}{required}{type=string array or scalar}
11; the string to be changed
12;
13; @param EXP1 {in}{required}{type=scalar string}
14; a single regular expression (as implemented by the STREGEX function).
15;
16; @param EXP2 {in}{required}{type=scalar string}
17; replacement character/string
18;
19; @keyword FOLD_CASE
20; Indicates that the regular expression matching should be done in a case-insensitive fashion.
21;
22; @returns
23; string array or scalar
24;
25; @examples
26;
27;   IDL> abc = 'abcdefghijklmnopqrstuvwxyz'
28;   IDL> print, strsed(abc, 'm', 'M')
29;    abcdefghijklMnopqrstuvwxyz
30;   IDL> print, strsed(abc, 'm.*t', 'M_T')
31;    abcdefghijklM_Tuvwxyz
32;   IDL> a = 'abcabcabc'
33;   IDL> help, strsed([abc, a, 'eee'], 'abc', 'XXX_')
34;    <Expression>    STRING    = Array[3]
35;   IDL> print, strsed([abc, a, 'eee'], 'abc', 'XXX_')
36;    XXX_defghijklmnopqrstuvwxyz XXX_XXX_XXX_ eee
37;
38; @history
39; Sept 2006: Sebastien Masson (smasson\@lodyc.jussieu.fr)
40;
41; @version
42; $Id$
43;-
44FUNCTION strsed, str, exp1, exp2, FOLD_CASE=fold_case
45;
46  compile_opt idl2, strictarrsubs
47;
48  strout = str
49  line = stregex(strout, exp1)
50  line = where(line NE -1, cnt)
51  IF cnt GT 0 THEN BEGIN
52    FOR i = 0L, cnt-1 DO $
53       strout[line[i]] = strjoin(strsplit(strout[line[i]], exp1, /extract $
54                                          , /regex, /preserve_null, FOLD_CASE = fold_case), exp2)
55  ENDIF
56;
57  return, strout
58END
Note: See TracBrowser for help on using the repository browser.