How to retrieve a Sub-string from a String

Problem Statement:  We have a requirement where the client wants a part of String from Sales Order No. which is Code(Data type) and it is auto-generated from Number Series. The Sales Order No. value is “SO/123/789/456”. The required Sub-string was: 123<Space>789.

Pre-requisites:

  • VS Code
  • AL Language Extension
  • Microsoft Dynamics NAV /  Business Central

Solution:

As per the requirement, we need to first retrieve Sub-strings “123” and “789” and this can be achieved using SELECTSTR( ) function. But SELECTSTR() only retrieves a Sub-string from a comma-separated String. And the Sales Order No. string is a slash-separated string. So firstly we need to convert this string to comma-separated string, which is achieved in the below-presented screen.

CONVERTSTR(String, From Characters, To Characters)

 

In the above Screen, “SalesOrder No.” is initialized in a text variable “SoNumber” and then is converted to a comma-separated string. The code is:

CONVERTSTR( String: Text, FromCharacters: Text, ToCharacters: Text )

Now comes the part where we retrieve Substring from this converted String using SELECTSTR( ) function as the string is now converted into a comma-separated string, below screen shows how it is done.

SELECTSTR(Number/Position, Comma-Separated String)

 

As you can see in the above screen how SELECTSTR() function is used to retrieve substring on the 3rd and 4th position of the String and stored in text variables “No1” and “No2” respectively. And these variables are then initialized to another variable with space.

The code is:

SELECTSTR(Position, Commastring);

In the above example the code is:

SELECTSTR(3, Separatenumber);
SELECTSTR(4, Separatenumber);

 

 

Conclusion:  We can retrieve a comma-separated string or any other character-separated string using CONVERTSTR() and SELECTSTR() functions.

Hope this helps!

 


Share Story :

SEARCH :

FOLLOW CLOUDFRONTS BLOG :

FOLLOW CLOUDFRONTS BLOG :

Enter your email address to follow this blog and receive notifications of new posts by email.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close