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

Last change on this file since 192 was 192, checked in by smasson, 18 years ago

several bugfix + new strsed

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