11 January 2011

working with TextBuffer class - separate values from string

static void TextBuffer(Args _args)
{
TextBuffer buffer;
str source;
;

source = "I am first,I am second,I am third";
buffer = new TextBuffer();
buffer.setText(source);

while (buffer.nextToken(0, ','))
{
info (buffer.token());
}
}

1 comment:

  1. You will run into problems if the seperator causes empty results.

    Example: using source = ",I am second,I am third,,I am fifth,,";
    7 fields, as there are 6 ',' seperators.

    static void TextBuffer2(Args _args)
    {
    TextBuffer buffer;
    str source;
    int startPos;
    int foundIdx = 0;
    ;

    source = ",I am second,I am third,,I am fifth,,";
    buffer = new TextBuffer();
    buffer.setText(source);
    foundIdx = 0;
    while (buffer.nextToken(0, ','))
    {
    foundIdx++;
    info (strfmt('%1 - %2', foundIdx, buffer.token()));
    }

    // old-school
    buffer = new TextBuffer();
    buffer.setText(source);
    startPos = 1;
    foundIdx = 0;
    while (buffer.find(',', startPos))
    {
    foundIdx++;
    info(strfmt('%1 - %2', foundIdx, buffer.subStr(startPos, buffer.matchPos()-startPos)));
    startPos = buffer.matchPos() + buffer.matchLen();
    }
    if (startPos <= buffer.size()+1)
    {
    foundIdx++;
    info(strfmt('%1.- %2', foundIdx, buffer.subStr(startPos, buffer.size()-startPos+1)));
    }
    }


    Output:
    1 - I am second
    2 - I am third
    3 - I am fifth
    1 -
    2 - I am second
    3 - I am third
    4 -
    5 - I am fifth
    6 -
    7.-

    Oldschool = bad, but correct.

    ReplyDelete

Give me the commetns and solutions

Ledger Voucher creation Framework and x++ code to create ledger voucher

 Please click her for MS reference file Below is the out of the box example reference and code. SalesInvoiceJournalPostSubBill_Extension->...