Use UI ////////////////////////////////////////////////////////////////////////////// // CLASS: StringTokenizer // VERSION: 1.0 (18.01.2000) // AUTHOR: Torkild Ulvøy Resheim // Copyright 2000 Emma EDB AS, Trondheim Norway //---------------------------------------------------------------------------- // An instance of class can be used to split a string into substrings at the // positions where the separator token exists. // // Example of use: // // Object st is a StringTokenizer // set Separator to "," // End_Object // Integer count counter // set ParseString of st to "This is a test, as you can see the string is split at the comma." // Get ItemCount of st to count // local string token // while counter lt count // get TokenAt of st counter to token // showln token // increment counter // end ////////////////////////////////////////////////////////////////////////////// Class StringTokenizer is a cUIObject // BaseClass Procedure Construct_Object Forward Send Construct_Object // The string to parse Property String psString "" // The token separator, default value is space Property String Separator " " // In case we don't want to zero the token array Property Boolean iDoNotZero False // An array to contain the tokens object tokens is an Array End_Object End_Procedure //-------------------------------------------------------------------------- // Sets the string and tokenizes it //-------------------------------------------------------------------------- procedure Set ParseString String st String sep String token integer current_token integer separator_pos get Separator to sep // Make sure we zero it If (iDoNotZero(Self) = False) Begin send delete_data to (tokens(self)) End Repeat // Get the separator position move (Pos(sep,st)) to separator_pos // Get part that is to the left of the separator position to a string // (the new token is the result) or use what is left of the string if (separator_pos=0) Begin move st to token End else Begin move (Left(st,separator_pos-1)) to token End // Store the string in the token array set array_value of (tokens(self)) current_token to token // Increment the token count increment current_token // Remove the new token from the original string Move (trim(Right(st,(Length(st)-separator_pos)))) to st // We repeat until the token is equal to the rest of the string (last token found) Until (st=token) End_Procedure //-------------------------------------------------------------------------- // Returns the number of tokens //-------------------------------------------------------------------------- function ItemCount returns Integer integer counter Array_id // get object id of array we are using move (tokens(self)) to array_id get item_count of array_id to counter Function_Return counter End_Function //-------------------------------------------------------------------------- // Returns the token at the given position //-------------------------------------------------------------------------- function TokenAt integer position returns string integer array_id string st move (tokens(self)) to array_id move (string_value(Array_id, position)) to st function_return st End_Function End_Class