1. ParseText

    Article: AN0001636Updated: 21.09.2018

    Object ParseText provides functions for a fast and comfortable text parsing, where particular lines contain values in format

    KEY = VALUE

    Such a text can be used for configurations or an automated data exchange among systems by means of text files or emails.

    The object has several configuration parameters, a method for text parsing and several functions for value reading.

     

    Name Description
    char KeyValueSeparator Separator between KEY and VALUE. Default value is =.
    string CommentIdentificator Identification of the line with a comment. All lines starting with this text will be ignored.
    string ParseRowOnlyStartWith Prefix for line parsing. If defined, only lines with this prefix will be parsed. The text is removed from the line before parsing.
    string DupliciteKey List of duplicate keys found during parsing. Only value from the first occurence is stored in the result.
    bool IsDupliciteKey Indication wheather a duplicate key was found.
    string ParseOnlyKeys List of enabled keys that shall be parsed. Everything will be ignored.
    void Parse(string text) Method for parsing. The input parameter text is the parsed text.
    bool ExistKey( string key) Function finds out, wheather the input key is in the results after parsing. Is so, True is returned, otherwise False.
    string GetString(string key) Function returns text value according to the input key. If there is no key in the parsing results, null is returned.
    string GetString(string key, string defaultValue) Function returns text value according to the input key. If there is no key in the parsing results, defaultValue is returned.
    int? GetInteger(string key) Function returns integer value according to the input key. If there is no key in the parsing results, null is returned.
    int? GetInteger(string key, int defaultValue) Function returns integer value according to the input key. If there is no key in the parsing results, defaultValue is returned.
    bool? GetBoolean(string key) Function returns yes/no indication according to the input key. If there is no key in the parsing results, null is returned.
    bool? GetBoolean(string key, bool defaultValue) Function returns yes/no indication according to the input key. If there is no key in the parsing results, defaultValue is returned.
    double? GetDouble(string key) Function returns decimal value according to the input key. If there is no key in the parsing results, null is returned.
    double? GetDouble(string key, double defaultValue) Function returns decimal according to the input key. If there is no key in the parsing results, defaultValue is returned.
    DateTime? GetDateTime(string key, string format) Function returns date value according to the input key. If there is no key in the parsing results, null is returned.
    DateTime? GetDateTime(string key, string format, DateTime defaultValue) Function returns date according to the input key. If there is no key in the parsing results, defaultValue is returned.

    Parametr format in the function GetDateTime defines the date format in the value VALUE of the input text. The format corresponds with the defintion of the DateTime data type parsing from .NET Framework. You can find more about the format in the Microsoft documentation.

    Example

    Script:

    var pt = OG.TextUtils.CreateparseText();
    pt.CommentIdentificator = '//';
    pt.Parse( text);
    var name = pt.GetString( 'name');
    var code = pt.GetString( 'code', '---');
    var count = pt.GetInteger( 'CPUCount', 0);

    Text for parsing:

    //transport text, format 1.2
    name = Opteron
    code = X10
    description = .....
    CPUCount = 47

×