In Microsoft Dynamics 365 Business Central, string functions are AL language functions for measuring, searching, extracting, changing, and formatting text. Common choices include StrLen, CopyStr, StrPos, DelChr, Format, and StrSubstNo; the right one depends on whether you are editing text, finding a value, or converting data for display.
Where string functions fit in Business Central
Business Central developers use string functions in AL code: for example, in table and page extensions, codeunits, reports, and integrations. They are not menu commands for changing text in the Business Central interface, nor are they Excel formulas. A function takes one or more values, performs an operation, and returns a value that AL code can use or assign to a variable.
Text may come from a user-entered field, a document number, an imported file, an API response, or a value that the application formats for display. A typical task might be to remove separators from an external reference, extract a prefix, check whether a phrase occurs, or build a readable message. AL provides small functions for these operations rather than requiring a developer to write every character-handling routine from scratch.
Before choosing a function, distinguish the data you have from the result you need. Searching for a substring is different from extracting it; changing the displayed form of a number is different from changing the number itself. Getting that distinction right prevents many common bugs.
Text and Code variables
Most AL string work involves the Text data type. A declaration such as Text[50] gives the variable a maximum length of 50 characters. The Code data type is intended for code-like values such as identifiers and codes. Unlike Text, a Code value is converted to uppercase and has leading and trailing spaces removed. That behavior can be useful for identifiers, but it is not appropriate when letter case or whitespace is meaningful.
Choose the type based on the business meaning, not just on which function you plan to call. A person’s name, a message, or a case-sensitive external token usually belongs in Text. A normalized product or customer code may be suitable for Code, if that matches the table and application design. Also consider the length of the destination field: a string function can produce a value that does not fit the variable or field to which you assign it, so define and validate lengths deliberately.
Core functions for measuring, searching, and extracting
These functions answer three basic questions: how long is the value, where does a fragment appear, and what part of the value should be returned?
StrLen(String)returns the current number of characters in a string. It is useful for checking whether a value is empty or whether it meets a length rule.StrPos(String, SubString)returns the position where the substring first occurs, or 0 when it is not found. Positions are counted from 1, not 0.CopyStr(String, Position, Length)returns a portion of a string, beginning at the specified position. The optional length can be left out when the intended result is the remainder of the string.MaxStrLenreports the maximum length of a bounded text or code variable. It is not a substitute forStrLen: one concerns the variable’s capacity, the other the current content.
For example, in CopyStr('BC-2026-041', 4, 4), the returned text is 2026. In StrPos('BC-2026-041', '2026'), the result is 4. A check such as if StrPos(ExternalRef, 'INV-') > 0 then tests whether a marker appears, but it does not by itself verify that the marker is at the beginning. For that, compare the beginning of the value or inspect the position explicitly.
Functions that change or assemble text
AL also has functions for removing, replacing, inserting, padding, and selecting text. These are useful when adapting values to a required format, but they should not replace validation of the underlying data.
DelChr(String, Where, Which)removes specified characters. TheWhereargument controls whether removal applies at the beginning, end, or throughout the string; use it carefully because removing every matching character can change meaningful content.ConvertStr(String, FromCharacters, ToCharacters)translates characters using corresponding positions in the two character lists. It is useful for straightforward character substitutions.InsStr(String, InsertString, Position)inserts text at a specified position.PadStr(String, Length, FillCharacter)pads text to a requested length using a fill character. Check the desired output length and the receiving field before using it.SelectStr(Number, String)returns an item from a comma-separated list, with selection numbering starting at 1. It is intended for simple lists, not for parsing arbitrary data that may itself contain commas.UpperCaseandLowerCasereturn text with letter case changed. Do not use these to normalize values when the original capitalization must be preserved.
For example, DelChr('A-10-4', '=', '-') removes every hyphen and returns A104. That may be appropriate for a comparison key, but it would be wrong if hyphens distinguish valid reference formats. If the goal is only to remove surrounding spaces, use leading- and trailing-character handling rather than deleting spaces throughout the value.
Formatting values and building messages
Format converts a value such as a date, number, or Boolean into text. The result is affected by the value’s type and formatting options, and presentation can be influenced by regional settings. This matters when text is shown to a user: a date or decimal may be displayed differently under different language or regional configurations. Do not assume that formatted display text is a stable machine-readable representation.
StrSubstNo builds a message from a format string and replacement values. Numbered placeholders such as %1 and %2 make messages easier to read and maintain than a long chain of concatenations. For example, StrSubstNo('Order %1 for %2', OrderNo, CustomerName) inserts the order number and customer name in their respective positions. Keep user-facing messages translatable and avoid relying on a particular word order; placeholders allow a translated message to arrange the supplied values appropriately.
Formatting is not the same as conversion for data exchange. If an integration needs a fixed date or number representation, define that format as part of the interface contract and account for locale and data type. When working with structured JSON or XML, use Business Central’s JSON or XML facilities rather than manually assembling punctuation and quoted values with string functions.
A practical approach to common string tasks
For a reference such as BC-2026-041, a developer can first check whether the value is long enough, then find a known separator or marker, and only then extract the intended segment. Each step should reflect the actual format rule. Simply taking characters 4 through 7 works only if every valid reference follows the same pattern.
A robust sequence for text processing is:
- Identify the input type and its source. Decide whether case, spaces, punctuation, and regional formatting carry meaning.
- Validate basic requirements, such as non-empty content, expected length, or the presence of a required marker.
- Use the narrowest function that expresses the operation: search with
StrPos, extract withCopyStr, or remove specific characters withDelChr. - Check the returned value before assigning it to a shorter variable or a table field. Handle unexpected input with an error message or a suitable fallback.
- Test boundary cases, including an empty string, a missing marker, a marker at the first or last position, and text at the maximum expected length.
For instance, if an imported reference is expected to begin with INV-, finding that text somewhere in the reference is not sufficient. A value such as OLD-INV-17 contains the marker but does not have the expected prefix. Validate the position and the full pattern before extracting a number or saving the result.
Common problems and misconceptions
One frequent mistake is mixing up the current length and the maximum length. Use StrLen to ask how many characters a value currently contains; use MaxStrLen when you need to know the capacity of a bounded variable. Another is treating positions as zero-based. AL string positions begin at 1, so calculations copied from a language that starts at 0 may select the wrong characters.
It is also easy to overuse destructive cleanup. Removing all spaces, punctuation, or accents may make two values compare equal when the business considers them different. Decide whether the change is for display, validation, storage, or matching, and preserve the original value when it may be needed later. Case conversion is not a substitute for a complete rule for comparing external identifiers.
Finally, do not assume that Format always yields the same text on every user’s machine, or that a formatted number can safely be parsed back without controlling its format. Display conventions such as decimal separators and date order vary. For integrations, use explicit, documented serialization rules instead of relying on a user’s display settings.
Choosing between AL string functions and other tools
Use AL string functions when the task is a focused operation on text within application logic. For structured payloads, use the appropriate JSON or XML data types and methods so escaping, field names, and data types are handled as structured data. For database filtering or record selection, use AL record filters and keys where appropriate rather than loading many values and searching them as strings.
Some developers arrive from C/AL, SQL, JavaScript, or Excel and expect identical names or behavior. Function names and indexing conventions differ between languages, and a similarly named function may have different parameters or return values. Check the AL documentation for the target Business Central runtime and verify assumptions in a small, representative test. For code that is part of an extension, keep the operation close to the business rule it supports and add tests for meaningful edge cases.
Frequently Asked Questions
What is the AL function for finding text in a string?StrPos(String, SubString) returns the first matching position, or 0 if the substring is not found. Its positions start at 1.
How do I get part of a string in Business Central?
Use CopyStr with the source text, starting position, and desired length. Confirm the position and length against the actual input format before relying on fixed offsets.
What is the difference between StrLen and MaxStrLen?StrLen measures the current text value. MaxStrLen reports the maximum length of a bounded text or code variable. They answer different questions.
How can I remove a character from AL text?
Use DelChr and specify both where removal should occur and which character or characters to remove. Decide whether removal should apply at the edges or throughout the value.
Does Business Central have a function to replace text?ConvertStr translates characters according to matching positions in two character lists. For more involved replacements or structured content, choose an approach suited to the data and confirm its behavior in AL documentation rather than assuming another language’s replacement function exists with the same syntax.
Related
- How to Subtotal Data in Excel and Summarize Groups
- How to Combine Parts in Onshape with Boolean Union
- Hide Column Headers in Power BI: Table & Matrix
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel Date Subtraction: Formulas, Days, and WorkdaysTo subtract one date from another in Excel, subtract the earlier date cell from the later one: if…
- Excel Formula for Days Left Till DateThe simplest Excel formula for days left until a date is =DATE(2025,12,31)-TODAY(), which returns the number of days…
- Split S2 into K Equal Parts in Excel: Formula GuideTo split the numeric value in Excel cell S2 into k equal parts, divide it by the number…
- Excel Timesheet Formula: Total Hours, Breaks, and ShiftsFor a timesheet with a start time in B2, an end time in C2, and an unpaid break…
- Large Excel Cheat Sheet: Formulas, Shortcuts & TipsA large Excel cheat sheet is a quick reference for common formulas, keyboard shortcuts, data tools, formatting, and…
- Excel Date Calculation Methods and FormulasIn Excel, calculate dates by adding or subtracting dates and day counts, or use functions such as DATE,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.