Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
tsa::string_splitter Class Reference

Support class for splitting 'record' strings into 'fields' using separator characters such as ','. More...

#include <TSAFile.h>

Inheritance diagram for tsa::string_splitter:
tsa::object

Public Member Functions

 string_splitter (void)
 Constructor.
 
virtual ~string_splitter (void)
 Destructor.
 
const variantoperator[] (size_t index) const
 Returns a Variant object, of type string corresponding to the field found at index.
 
size_t size (void) const
 Returns the number of fields found in the string that was split, which also equals the size of the Variant array containing the field values.
 
size_t split (const char *string_to_split, char separator)
 Splits string_to_split using a separator character such as ','. More...
 
- Public Member Functions inherited from tsa::object
 object (void)
 Constructor.
 
virtual ~object (void)
 Destructor.
 
const char * class_name (void) const
 Returns the class name as returned by the 'Real Time Type Information' (RTTI) mechanism. More...
 
bool has_same_class_as (const object &other) const
 Returns true if self has the same type as other object. More...
 

Detailed Description

Support class for splitting 'record' strings into 'fields' using separator characters such as ','.

Member Function Documentation

size_t tsa::string_splitter::split ( const char *  input_str,
char  sep 
)

Splits string_to_split using a separator character such as ','.

Returns
The function returns the number of fields found.
Note
This class is useful when reading records, often from CSV (comma separated value) files. With the help of this class, records can be split into individual fields and placed into an internal array of Variant objects. The array is automatically sized to fit the number of fields found. Regardless of the type represented by the value, all Variant objects will be of type string, but can easily be converted to any other type using class Variant's many conversion operators.

If stringToSplit contains two consecutive separator characters, then they are considered to delimit an empty field. Likewise, if a separator is the first or last character in stringToSplit, an empty field is assumed to exist in first or last position!

Examples
String text = "one,two,3,4,5.5";
StringSplitter record;
record.Split(text, ','); //splitting text using ',' as separator
a(record.GetSize() == 5); //found 5 fields in record
//StringSplitter populates an internal array of CVariants which are accessed
//using operator[]. See class Variant for casting operators.
a( (String)record[0] == "one");
a( (String)record[1] == "two");
a( (String)record[2] == "3");
a( (int)record[3] == 4); //cast to int
a( (double)record[4] == 5.5); //cast to double