Sunday, January 4, 2015

DECSTR (like INCSTR but decreasing)

Hi all,

This post is about a function that we can create the opposite of INCSTR .
In dynamics NAV "INCSTR" is used for Increases a positive number or decrease a negative number inside a string by one (1).

Ref. http://msdn.microsoft.com/en-us/library/dd338972.aspx

For example : 
NewString := INCSTR(String)

Parameters

String
Type: Text constant or code
The string that you want to increase or decrease.

Property Value/Return Value

Type: Text constant or code
The result after increasing or decreasing String.
 
 However, we can developed a function which will provide the opposite of INCSTR. For some cases we need to used it.

 Here is the function code.
A function to descrease a string:

DecStr(IcodIncoming : Code[20];IintStep : Integer) OcodReturnCode : Code[20]
// DecStr //> 104
// Like the NAVISION INCSTR-function but DECREASING
// PARAMETERS:
// IcodIncoming : String to be decreased (if this string does not contain a number, an empty string is returned)
// IintStep : Decrement with this value (if it is negative, an empty string will be returned)
// (if it is 0, the value of "IcodIncoming" will be returned)
// RETURN-VALUE : decreased string

// in some cases, just exit
IF IintStep = 0 THEN
EXIT(IcodIncoming);
IF IintStep < 0 THEN
EXIT('');
IF IcodIncoming = '' THEN
EXIT('');

LcodAllowedChars := '1234567890';
LintLen := STRLEN(IcodIncoming);
LintStartOfNumericPart := LintLen;

// search the last not numeric character
WHILE (STRPOS(LcodAllowedChars,COPYSTR(IcodIncoming,LintStartOfNumericPart,1)) <> 0) DO
LintStartOfNumericPart -= 1;

// start of numeric part
LintStartOfNumericPart += 1;

IF LintStartOfNumericPart > LintLen THEN
EXIT(''); // no numeric part found at right end of the string

EVALUATE(Lint,COPYSTR(IcodIncoming,LintStartOfNumericPart));

Lint -= IintStep;

IF Lint < 0 THEN
ERROR(LtxcNegative,IcodIncoming,IintStep,Lint);

LcodAllowedChars := FORMAT(Lint);

OcodReturnCode :=
COPYSTR(IcodIncoming,1,LintStartOfNumericPart - 1) +
PADSTR('',LintLen - LintStartOfNumericPart - STRLEN(LcodAllowedChars) + 1,'0') +
LcodAllowedChars;

Thank you
Regards,
Dinuka.

No comments:

Post a Comment