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.
Add a function called
RemoveNonNumeric()
. It should return a text variable namedNewString
.The function should take in the following parameter:
Name
Type
Length
String
Text
30
Add the following global variable:
Name
Type
I
Integer
Add the following global variables:
Name
Type
Length
OldPhoneNumber
Text
30
NewPhoneNumber
Text
30
Add 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;
Add the following code to the
OnRun
trigger:OldPhoneNumber := '(404) 555-1234'; NewPhoneNumber := RemoveNonNumeric(OldPhoneNumber); MESSAGE('Old Phone Number: %1\New Phone Number: %2', OldPhoneNumber, NewPhoneNumber);
When you run the codeunit you will see a window similar to 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 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 numeric, we add it to our resulting string.
NAV comes with plenty of built-in string manipulation functions to remove characters, return substrings, find characters within string, and many more. A search in the C/SIDE Reference Guide from the NAV client help menu for string functions will give you a complete list.
Parsing strings has several uses in NAV. Some easy-to-implement examples include checking/converting a phone number to a proper format based on country code, properly capitalizing names, and removing illegal characters.
Using the Object Designer run table 6508, Value Entry Relation. You should see a column named Source RowId that contains some strange looking text. A careful examination reveals that these are not as strange as they appear. It is simply a string containing six values, each separated by a semicolon and enclosed within quotes. For example: "123";"0";"123456";""; "0";"10000".
In a typical installation involving shipments and receipts, the value of the current inventory is adjusted every time an item comes in or goes out of stock. This amount is stored in the Value Entry table. In order to know which document created which value entry, a subsidiary table was created: Value Entry Relation. In this basic scenario, the first field refers to the table that the value entry came from. The most common are: 113 for shipments and 123 for receipts. The third value stores the document number and the sixth contains the line number. Take a look at the function DecomposeRowID()
in codeunit 6500, Item Tracking Management.
FOR ArrayIndex := 1 TO 6 DO Item Tracking ManagementStrArray[ArrayIndex] := ''; Len := STRLEN(IDtext); Pos := 1; ArrayIndex := 1; WHILE NOT (Pos > Len) DO BEGIN Char := COPYSTR(IDtext,Pos,1); IF (Char = '"') THEN BEGIN Write := FALSE; Count += 1; END ELSE BEGIN IF Count = 0 THEN Write := TRUE ELSE BEGIN IF Count MOD 2 = 1 THEN BEGIN Next := (Char = ';'); Count -= 1; END ELSE IF NoWriteSinceLastNext AND (Char = ';') THEN BEGIN Count -= 2; Next := TRUE; END; Count /= 2; WHILE Count > 0 DO BEGIN StrArray[ArrayIndex] += '"'; Count -= 1; END; Write := NOT Next; END; NoWriteSinceLastNext := Next; END; IF Next THEN BEGIN ArrayIndex += 1; Next := FALSE; END; IF Write THEN StrArray[ArrayIndex] += Char; Pos += 1; END;
This is an amazing example of how you can manipulate strings to your advantage. The code is fairly complex and may take some time to understand, but it can give you a basis to write your own code. You should be able to see the code that looks for semicolons, or field separators, as well as the code that finds quotes, or field identifiers. The code separates out those fields and stores them in a string array for later use.