Searching for Multiple Occurrences Design Pattern
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
Use the following pattern when you search through a text for multiple (all the) occurrences of a search expression.
int startPos;
TextBuffer textBuffer = new TextBuffer();
;
textBuffer.setText(/*Your text goes here*/);
startPos = 0;
while (textBuffer.find(/*Your search expression goes here*/,startPos))
{
/* Do whatever is needed with the text found.
The text is found in the buffer contents
from textBuffer.matchPos() and textBuffer.matchLen() ahead */
...
startPos = textBuffer.matchPos() + textBuffer.matchLen();
}
Tip
By default, the search expression is a regular expression.
Example
The following illustrates an example of the multiple occurences design pattern.
static void textBufferFind(Args _args)
{
int startPos;
TextBuffer textBuffer = new TextBuffer();
;
textBuffer.setText("Hello World");
startPos = 0;
while (textBuffer.find('o',startPos))
{
print textBuffer.matchPos(),' ',textBuffer.matchLen();
startPos = textBuffer.matchPos() + textBuffer.matchLen();
}
pause;
}
See also
Microsoft Dynamics AX Design Patterns
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.