Improved strSplit() method in X++

How String.Split() works in C#

Check the Microsoft documentation on String.Split() >>

The optional parameter of type StringSplitOptions allows you to specify options such as whether to omit empty substrings from the returned array or trim whitespace from substrings. This enumeration supports a bitwise combination of its member values.

How strSplit() works in X++

It works in the same way as String.Split() with the StringSplitOptions.None parameter - it splits the input string into a list of substrings delimited by elements in the specified delimiter string. This means that it doesn't trim the substrings nor omit empty substrings from the resulting list, so you can get:

strSplit("a ; b ;; ", ";") -> List {"a ", " b ", "", " "}

The question is, what to do if we want to get the following result instead:

List {"a", "b"}

Of course, we can always use String.Split() in X++ as well, but for the sake of simplicity of the use of the method and to avoid dealing with .NET Arrays for input delimiters and the returning array of substrings, we have created our "improved" version of strSplit() in x++. Let's take a look!

We made strSplit() working as in C#

Implementation

We introduce a similar enum to StringSplitOptions in .NET - enum DocStringSplitOptions:

  • None (0) - the default mode, which doesn't trim the substrings nor omit empty substrings from the resulting list
  • RemoveEmptyEntries (1) - omit all substrings that contain an empty string from the resulting list
  • TrimEntries (2) - trim white-space characters from each substrings in the resulting list

If RemoveEmptyEntries and TrimEntries are specified together, then the substrings that consist only of white-space characters are also removed from the resulting list.

Unit tests

This method and many more utilities are part of our Docentric Free Edition. Why wait? Download and start using it right away 😀

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Docentric respects your privacy. Learn how your comment data is processed >>

Docentric respects your privacy. Learn how your comment data is processed >>