It can be very useful to parse a string and retrieve certain values. This recipe will show you how to examine the contents of a string and manipulate that data.
Let's create a new codeunit from Object Designer.
Add a function called
RemoveNonNumeric
. It should return a text variable calledNewString
.Add the following parameters for the same function:
Name
Type
Length
String
Text
30
Now add the following global variables:
Name
Type
Length
OldPhoneNumber
Text
30
NewPhoneNumber
Text
30
I
Integer
Write the following code to the
RemoveNonNumeric
function:FOR i := 1 TO STRLEN(String) DO BEGIN IF String[i] IN ['0', '1', '2', '3', '4', '5', '6', '7','8','9'] THEN NewString := NewString + FORMAT(String[i]); END;
Write the following code into the
OnRun
trigger of the codeunit:OldPhoneNumber := '(230) 299-876'; NewPhoneNumber := RemoveNonNumeric(OldPhoneNumber); MESSAGE('Old Phone Number: %1\New Phone Number: %2', OldPhoneNumber, NewPhoneNumber);
To complete the task, save and close the codeunit.
On executing the codeunit, you should see a window similar to the one shown in the following screenshot:
A string is actually an array of characters. The same array syntax will be used to access the individual characters of the string.
We start with a FOR
loop that begins at the first character, with index 1
, and goes on until we reach the end of our string. This is determined using the STRLEN()
function, which stands for string length. As the first index is 1
, the last index will be N
or the number of characters in the string.
Next, we access the character at that index using square brackets. If the character is a number, meaning we want to keep it because it is a numeric value, we add it to our resulting string.
Note
NAV comes with plenty of built-in string manipulation functions to remove characters, return substrings, find characters within strings, and many more. A search in the Developer and IT Pro Help option of the Help menu of Microsoft NAV Development Environment for string functions will give you a complete list.
The CheckIBAN
function of the 79
, Company Information
table is a simple example of string manipulation to validate
IBAN (International Bank Account Number). IBAN is internationally agreed on and adopted. It consists of up to 34 alphanumeric characters: the first two letters are the country code, then two check digits, and finally a country-specific Basic Bank Account Number. The same is validated by manipulating the input string using various functions. The following code gives you an example for the same:
IF IBANCode = '' THEN EXIT; IBANCode := DELCHR(IBANCode); Modulus97 := 97; IF (STRLEN(IBANCode) <= 5) OR (STRLEN(IBANCode) > 34) THEN IBANError; ConvertIBAN(IBANCode); WHILE STRLEN(IBANCode) > 6 DO IBANCode := CalcModulus(COPYSTR(IBANCode,1,6),Modulus97) + COPYSTR(IBANCode,7); EVALUATE(I,IBANCode); IF (I MOD Modulus97) <> 1 THEN IBANError;
There are a few more functions used to validate the string; such as ConvertIBAN
, CalcModulus
, and ConvertLetter
. These functions can give you a basic idea to write your own code.
For more complex examples, please follow the
DecomposeRowID()
function in the 6500
, Item Tracking Management
codeunit. The code evaluates the value stored in the Source RowId
field of the 6508
, Value Entry Relation
table.