source: trunk/SRC/ToBeReviewed/STRING/strtok.pro

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

improvements of headers (examples and alignments)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1;+
2;
3; @file_comments
4; Retrieve portion of string up to token.
5;
6; @categories
7; String
8;
9; @param STRING {in}{required}
10; String to be split. Contains text after in, out token on output.
11;
12; @param TOKEN {in}{required}
13; Token to use in splitting old.
14;
15; @keyword TRIM
16; set to remove leading blanks from old before returning.
17;
18; @keyword HELP
19; print useful message and exit.
20;
21; @returns
22; new   -- portion of string up to token.              out
23; old   -- portion of old after token.                 out, in
24;
25; @restrictions
26; Input parameter old is modified.
27; Token may be one or more characters.
28; if token is not found, returns old and sets old to ''.
29;
30; @examples
31; If old is 'foo44 bar', then strtok( old, '44' ) would return
32; 'foo', and upon return, old will be left with ' bar'.  If /TRIM
33; were set, old would be 'bar' on return.
34;
35; not ok ++
36;   IDL> old='foo44 bar'
37;   IDL> res=strtok( old, '44' )
38;   IDL> print,res
39;       0           5
40;   IDL> print,old
41;      foo44 bar
42;
43;   IDL> old='foo44 bar'
44;   IDL> res=strtok( old, '44',/TRIM )
45;   IDL> print,res
46;       0           5
47;   IDL> print,old
48;      foo44 bar
49;
50; not ok ++
51; If old='xyz', then new=strtok(old,'a') would return with
52; new='xyz' and old=''.
53;   IDL> old='xyz'
54;   IDL> new=strtok( old, 'a' )
55;   IDL> print,new
56;           0
57;   IDL> print,old
58;   xyz
59
60;
61; @history
62;       $Log: strtok.pro,v $
63;       Revision 1.3  1996/06/14 20:00:27  mcraig
64;       Updated Copyright info.
65;
66;       Revision 1.2  1996/05/09 00:22:17  mcraig
67;       Added built in help.
68;
69;       Revision 1.1  1996/01/31 18:47:37  mcraig
70;       Initial revision
71;
72; Thanks:
73;       To D. Linder who wrote GETTOK, part of the goddard library,
74;       upon which this is based.
75;
76; Release:
77;       $Name: Rel_1_2 $
78;
79; Copyright:
80;  Copyright (C) 1996 The Regents of the University of California, All
81;  Rights Reserved.  Written by Matthew W. Craig.
82;  See the file COPYRIGHT for restrictions on distributing this code.
83;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
84;
85; @version
86; $Id$
87;
88;-
89FUNCTION strtok, string, token, TRIM=trim, HELP=Help
90;
91  compile_opt idl2, strictarrsubs
92;
93
94; Back to the caller if error occurs.
95    On_error, 2
96
97    IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN
98        offset = '   '
99        print, offset+'Retrieve portion of string up to token.'
100        print, offset+'new = strtok( old, token )'
101        print, offset+'Inputs:'
102        print, offset+offset+'old   -- String to be split.  Contains text after    in, out'
103        print, offset+offset+'         token on output.'
104        print, offset+offset+'token -- Token to use in splitting old.              in'
105        print, offset+'Keywords:'
106        print, offset+offset+'/TRIM -- set to remove leading blanks from old '
107        print, offset+offset+'         before returning.'
108        print, offset+offset+'/HELP -- print useful message and exit.'
109        print, offset+'Outputs:'
110        print, offset+offset+'new   -- portion of string up to token.              out'
111        print, offset+offset+'old   -- portion of old after token.                 out, in'
112        print, offset+'Side effects:'
113        print, offset+offset+'Input parameter old is modified.'
114        print, offset+'Notes:'
115        print, offset+offset+'Token may be one or more characters.'
116        print, offset+offset+"If token is not found, returns old and sets old to ''."
117        print, offset+'Examples:'
118        print, offset+offset+"If old is 'foo44 bar', then strtok( old, '44' ) would return'"
119        print, offset+offset+"  'foo', and upon return, old will be left with ' bar'.  If /TRIM"
120        print, offset+offset+"  were set, old would be 'bar' on return."
121;'
122        print, offset+offset+"If old='xyz', then new=strtok(old,'a') would return with"
123        print, offset+offset+"  new='xyz' and old=''."
124        return, -1
125    ENDIF
126
127    pos = strpos(string, token)
128
129    IF (pos GE 0) THEN BEGIN
130        front = strmid(string, 0, pos)
131        string = strmid(string, pos + strlen(token), strlen(string))
132        IF keyword_set(trim) THEN string = strtrim(string, 1)
133        return, front
134    ENDIF
135
136    front = string
137    string = ''
138    return, front
139
140END
Note: See TracBrowser for help on using the repository browser.