source: trunk/SRC/Textoidl/str_token.pro @ 378

Last change on this file since 378 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1;
2;+
3; NAME:
4;       STR_TOKEN
5; PURPOSE:
6;       Retrieve portion of string up to token.
7; CATEGORY:
8;       text/strings
9; CALLING SEQUENCE:
10;       new = str_token( old, token )
11; INPUTS:
12;       old   -- String to be split.  Contains text after    in, out
13;                token on output.
14;       token -- Token to use in splitting old.              in
15; KEYWORD PARAMETERS:
16;       /TRIM -- set to remove leading blanks from old
17;                before returning.
18;       /HELP -- print useful message and exit.
19; OUTPUTS:
20;       new   -- portion of string up to token.              out
21;       old   -- portion of old after token.                 out, in
22; COMMON BLOCKS:
23; SIDE EFFECTS:
24;       Input parameter old is modified.
25; NOTES:
26;       Token may be one or more characters.
27;       If token is not found, returns old and sets old to ''.
28; EXAMPLE:
29;       If old is 'foo44 bar', then str_token( old, '44' ) would return
30;       'foo', and upon return, old will be left with ' bar'.  If /TRIM
31;       were set, old would be 'bar' on return.
32;
33;       If old='xyz', then new=str_token(old,'a') would return with
34;       new='xyz' and old=''.
35; THANKS:
36;       To D. Linder who wrote GETTOK, part of the goddard library,
37;       upon which this is based.
38; MODIFICATION HISTORY:
39;       $Id$
40;       $Log: str_token.pro,v $
41;       Revision 1.1  2000/06/14 19:09:22  mcraig
42;       Changed name of strtok str_token to avoid conflict in IDL 5.3.
43;
44;       Revision 1.3  1996/06/14 20:00:27  mcraig
45;       Updated Copyright info.
46;
47;       Revision 1.2  1996/05/09 00:22:17  mcraig
48;       Added built in help.
49;
50;       Revision 1.1  1996/01/31 18:47:37  mcraig
51;       Initial revision
52;
53; RELEASE:
54;       $Name: Rel_2_1_2 $
55;
56; COPYRIGHT:
57;  Copyright (C) 1996 The Regents of the University of California, All
58;  Rights Reserved.  Written by Matthew W. Craig.
59;  See the file COPYRIGHT for restrictions on distrubting this code.
60;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
61;-
62FUNCTION str_token, string, token, $
63                 TRIM=trim, HELP=Help
64;
65  compile_opt idl2, strictarrsubs
66;
67
68; Back to the caller if error occurs.
69    On_error, 2
70
71    IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN
72        offset = '   '
73        print, offset+'Retrieve portion of string up to token.'
74        print, offset+'new = str_token( old, token )'
75        print, offset+'Inputs:'
76        print, offset+offset+'old   -- String to be split.  Contains text after    in, out'
77        print, offset+offset+'         token on output.'
78        print, offset+offset+'token -- Token to use in splitting old.              in'
79        print, offset+'Keywords:'
80        print, offset+offset+'/TRIM -- set to remove leading blanks from old '
81        print, offset+offset+'         before returning.'
82        print, offset+offset+'/HELP -- print useful message and exit.'
83        print, offset+'Outputs:'
84        print, offset+offset+'new   -- portion of string up to token.              out'
85        print, offset+offset+'old   -- portion of old after token.                 out, in'
86        print, offset+'Side effects:'
87        print, offset+offset+'Input parameter old is modified.'
88        print, offset+'Notes:'
89        print, offset+offset+'Token may be one or more characters.'
90        print, offset+offset+"If token is not found, returns old and sets old to ''."
91        print, offset+'Examples:'
92        print, offset+offset+"If old is 'foo44 bar', then str_token( old, '44' ) would return'"
93        print, offset+offset+"  'foo', and upon return, old will be left with ' bar'.  If /TRIM"
94        print, offset+offset+"  were set, old would be 'bar' on return."
95;'
96        print, offset+offset+"If old='xyz', then new=str_token(old,'a') would return with"
97        print, offset+offset+"  new='xyz' and old=''."
98        return, -1
99    ENDIF
100
101    pos = strpos(string, token)
102
103    IF (pos GE 0) THEN BEGIN
104        front = strmid(string, 0, pos)
105        string = strmid(string, pos + strlen(token), strlen(string))
106        IF keyword_set(trim) THEN string = strtrim(string, 1)
107        return, front
108    ENDIF
109   
110    front = string
111    string = ''
112    return, front
113   
114END
115
Note: See TracBrowser for help on using the repository browser.