The X++ string split function is str2con(), which splits a string into a container of substrings based on a delimiter. For example, str2con("a,b,c", ",") returns the container ["a", "b", "c"].
Background: Why X++ Has No Built-in Split()
X++ is the object-oriented programming language used in Microsoft Dynamics 365 Finance and Operations (and earlier AX versions). Unlike C# or Java, X++ does not have a dedicated Split() method on its string type. Instead, the language provides a set of global string functions in the Global class, and str2con() is the standard way to break a string into parts. This design dates back to the early days of Axapta, where containers were a lightweight, built-in data type used for many list-like operations. Understanding this background helps you see why str2con() returns a container rather than a list or array—containers are the idiomatic X++ structure for such tasks.
How str2con() Works
The syntax is str2con(str _text, str _delimiter = ","). It takes two arguments: the string to split and a delimiter (default is a comma). It returns a container where each element is a substring. The delimiter is case-sensitive and treated as a literal string, not a regular expression. For example:
str2con("one;two;three", ";")returns["one", "two", "three"]str2con("2024-01-15", "-")returns["2024", "01", "15"]str2con("no delimiter here")returns["no delimiter here"](the whole string as one element)
If the delimiter appears at the start or end of the string, str2con() includes empty strings in the result. For instance, str2con(",a,", ",") returns ["", "a", ""]. This behavior is important to remember when parsing user input or CSV lines where trailing commas might exist.
How to Access the Split Parts
Since str2con() returns a container, you access its elements using the conPeek() function, and you get the number of elements with conLen(). Here is a typical loop:
str input = "apple,banana,cherry";
container parts = str2con(input, ",");
int i;
for (i = 1; i <= conLen(parts); i++)
{
info(conPeek(parts, i));
}
This prints each fruit on a separate line in the Infolog. Note that container indices start at 1, not 0. If you need to convert the container to a list or array, you can use ContainerHelper::containerToList() or iterate manually.
Choosing the Right Delimiter and Handling Edge Cases
When splitting strings, the delimiter choice is critical. Common delimiters include commas, semicolons, pipes, and newlines. For newline splitting, you can use str2con(text, "\n"), but be aware that line endings may be \r\n in Windows. In that case, you might need to replace \r\n with \n first using strReplace(). Also, if your data contains the delimiter inside quoted fields (e.g., CSV with commas inside quotes), str2con() will not handle that correctly—it splits naively. For robust CSV parsing, consider using the CSVFile class or a custom parser.
Common Problems and Misconceptions
One frequent mistake is assuming str2con() trims whitespace. It does not. str2con("a, b", ",") returns ["a", " b"]—note the leading space in the second element. To trim each part, you must call strLRim() or strTrim() on each element. Another misconception is that str2con() accepts a regular expression; it does not. If you need regex splitting, you have to use System.String.Split() via .NET interop or write a custom function. Also, some developers try to use strSplit(), but that function does not exist in X++—the correct name is str2con().
Alternatives: .NET Interop and Other Methods
If you need more advanced splitting (e.g., multiple delimiters or regex), you can use .NET interop. For example:
System.String netStr = "a;b,c";
System.Char[] delimiters = new System.Char[2]();
delimiters.SetValue(';',0);
delimiters.SetValue(',',1);
System.String[] parts = netStr.Split(delimiters);
This gives you a .NET array that you can iterate with a foreach loop. However, this approach is heavier and less idiomatic. Another alternative is to use the strToken() function, which extracts a specific token from a string based on a delimiter and an index, but it does not return all tokens at once. For simple cases, str2con() is the recommended and most efficient method.
Performance and Best Practices
str2con() is generally fast for typical string lengths. However, if you are splitting very large strings (e.g., megabytes of text), consider using .NET interop or a streaming approach to avoid memory overhead. Also, be mindful of the container size limit—containers can hold up to 2^31 elements, but practical limits are lower. For code clarity, always specify the delimiter explicitly even if you want a comma, and handle empty elements if they are possible. When using the result, avoid repeated conPeek() calls inside loops that also modify the container; instead, copy elements to a temporary variable.
Frequently Asked Questions
How do I split a string by space in X++?
Use str2con(str, " "). For example, str2con("Hello world", " ") returns ["Hello", "world"]. Note that multiple consecutive spaces will produce empty elements; you may want to use str2con(strReplace(str, " ", " "), " ") to collapse them.
Does str2con() work with Unicode characters?
Yes, str2con() works with Unicode strings as long as the delimiter is also a Unicode string. The function operates on the raw characters, so it handles accented characters, emojis, and other symbols correctly.
Can I split a string into a List instead of a container?
Yes. You can convert the container to a list using ContainerHelper::containerToList(str2con(...)). This returns a List of strings, which you can iterate with ListEnumerator.
What is the difference between str2con() and strToken()?
str2con() returns all tokens as a container, while strToken() returns a single token at a specified index. For example, strToken("a,b,c", 2, ",") returns “b”. Use strToken() when you only need one specific part and want to avoid building a full container.
How do I split a string by newline in X++?
Use str2con(text, "\n"). If your text uses Windows line endings (\r\n), first replace them: str2con(strReplace(text, "\r\n", "\n"), "\n").
Why does str2con() return an empty element for trailing delimiters?
Because the function splits at every occurrence of the delimiter, including the one at the end. If you want to ignore empty trailing elements, you can filter them out after splitting using a loop or ContainerHelper::containerRemoveEmpty().
Related
- Java String matches(): How Regex Matching Works
- MS Access: How to Split a Database for Multiple Users
- Java String Length: How the length() Method Really Works
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Customer List Excel Free Template: What to IncludeYes—you can make a free customer list template in Excel by adding a header for each type of…
- Goal Seek in Excel: How to Find a Target ValueIn Excel for Microsoft 365 on Windows, open Data > What-If Analysis > Goal Seek. Choose a formula…
- Add Slide Numbers in PowerPoint: A Step-by-Step GuideTo add slide numbers in PowerPoint for Microsoft 365 on Windows, go to Insert > Header & Footer,…
- Excel Find and Replace Blanks: How to Fill or ClearTo fill genuinely empty cells in Excel, select the range, choose Home > Find & Select > Go…
- How to Insert a Divider Line in Word: 3 Easy MethodsTo insert a quick divider in Word, type three hyphens (—) on a blank line and press Enter;…
- Excel Shortcut for Lowercase Text: What Actually WorksExcel for Microsoft 365 on Windows has no built-in keyboard shortcut that changes selected text to lowercase. The…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.