Nuri Ensing
Nuri Ensing

Reputation: 2030

Splitting a specific bunch of text - JAVA

If I have this element:

    <Comments type="ITEM_OUT_COMMENTS" xml:lang="en">Item text
 203871: ATAG ZON POMPUPR 15-60 DO NOT DELETE SupplierAuxiliaryPartID : 395@@!817@@!N

Material PO text

Computers, Mainframe
COMPUTERS,MAINFRAME,SOURCED

                </Comments>

Is it also possible to get only this piece of text back: 395@@!817@@!N

This piece of text is always to be found behind: SupplierAuxiliaryPartID : But it can happen that there are no spaces like this SupplierAuxiliaryPartID:395@@!817@@!N:

   <Comments type="ITEM_OUT_COMMENTS" xml:lang="en">Item text
     203871: ATAG ZON POMPUPR 15-60 DO NOT DELETE SupplierAuxiliaryPartID:395@@!817@@!N

    Material PO text

    Computers, Mainframe
    COMPUTERS,MAINFRAME,SOURCED

                    </Comments>

I tried several splits but every time I cannot get the right piece of text.

Upvotes: 0

Views: 43

Answers (1)

Pedro Borges
Pedro Borges

Reputation: 1719

final String LABEL = "SupplierAuxiliaryPartID"

String getSupplierAuxiliaryPartId(String comments)
{
    // Split comments by line
    for (String line : comments.split('\n'))
    {
        // find where the label is
        int index = line.indexOf(label);

        if (index == -1)
        {
            // no label on this line
            continue;
        }

        // find first colon after the label
        index = line.indexOf(":", index + LABEL.length);

        if (index == -1)
        {
            // label without colon, but maybe the next line has a valid one
            continue;
        }

        // return the remaining of the line after the colon striping out extra whitespaces
        return line.substring(index + 1).trim();
    }

    // label with column not present in comment
    return null;
}

Upvotes: 1

Related Questions