Code highlighting

Wednesday, February 24, 2010

Casing and text search/comparison tutorial

I have recently received a question from one of my blog readers.
He was asking about the possibility of doing case-sensitive search in AX.

I would like to reply to that by creating a small tutorial on search in AX. It is definitely not going to cover all the scenarios, but will give beginners a basic understanding of their options.

Download the tutorial xpo from my Skydrive

The tutorial consists of 1 form with 3 tab pages.

Comparing two strings in AX is very simple: You can basically use the equality operator "==". As you can see from the tutorial, this is the case-insensitive operations, so "vanya" is equal to "Vanya".
AX also supports case-sensitive comparison. Kernel function strCmp() compares two strings taking into account the casing of the symbols in the string.

Searching for a substring is a common operation in AX.
For that you have a number of options, as usual:
strScan() function ignores casing and allows you to specify from which position to search, and how many symbols. This is very basic, and a method like this is present in any programming language.
TextBuffer.find() is a more advanced use of the search mechanism. First of all, it allows to ignore or take into account the casing in the source text. Similar to strScan, it allows to specify the start position for the search.
What it also has is support for Regular Expressions, as well as the ability to, for example, paste the text to Windows clipboard.
Lastly, there is the match() function. The main purpose of it is to find a match based on the specified pattern using regular expressions, but nothing prevents using it for a simple search operation. It has a rather limited output though. You only get a boolean value stating whether a match was found or not, while with the previous 2 methods you also get the position of the substring.

An interesting discovery that I made when writing the tutorial was about the speed of the different search operations. I have included this into the tutorial, so you can go in and try it yourself on your specific setup.
On my box, strScan() was the slowest operation of all, while TextBuffer, which I considered to be a very heavy class, was performing rather well.

Of course, single operation time compared to database operations is very low, so you won't notice it in your daily work. But it is something to think about.

Finding a symbol in a string based on a specified set of symbols is also possible.
You have 2 functions at your disposal for that: strFind and strNFind. The difference is that strNFind searches for any symbol NOT present in the provided set, compared to strFind.

This is not an extensive list, so I would be interested in hearing which functions you use or what your results for performance comparison would be.


  1. You forgot the "like" operator:
    The following works for example to check for a trailing "s" and then remove it.

    if(myStr like "*s")
    myStr = substr(myStr,1,strlen(myStr)-1);

  2. This has saved my day.
    I was able to find a RegEx in a string using match() but I couldn't extract the matched RegEx string. TextBuffer worked nicely.
    Thanks again.

  3. Great! thanx
    I was working on remove accents and ignoreCase property was useful

  4. Quote "[TextBuffer.match()] has a rather limited output though. You only get a boolean value stating whether a match was found or not, while with the previous 2 methods you also get the position of the substring."

    Actually found that you can retrieve the start position of the substring by calling textBuffer.matchPos().

  5. Hi Bogdana,

    Thanks for the comment. Actually, I am talking about the match() function there, not about the TextBuffer.find() (TextBuffer.match does not exist). You can find it under SystemDocumentation\Functions.
    You comment applies to the TextBuffer method, which is also described above.


Please don't forget to leave your contact details if you expect a reply from me. Thank you