Text Manipulation using .NET

Dynamics NAV offers some great built in string manipulation tools, like COPYSTR and PADSTR, but sometimes you need more, and as a result you end up writing more lines of code and using more variables than necessary.

But if you use the System.String class from .NET, instead of Text variables. Then you get more power and functions directly from the class.
Just check the Symbol Menu to see all the methods.

Let’s take some examples.
Create a variable called Input of type DOTNET and subtype System.String from the mscorlib namespace.


// System.String class is used as a normal Text variable
// It also maps one to one with Text datatype
Input := 'Here is som input text to play with';

// Take the first 20 characters from a string (like COPYSTR)
TextVariable := Input.Substring(0, 20);

// PadLeft (like PADSTR)
CharVariable := ' '; // FillCharacter
TextVariable := Input.PadLeft(Length, CharVariable);

// PadRight (you can't do this in NAV)
CharVariable := '#'; // FillCharacter
TextVariable := Input.PadRight(50, CharVariable);

// Search string
BoolVariable := Input.Contains('play');

// Check if string starts or ends with a specific substring 
BoolVariable := Input.StartsWith('Here');
BoolVariable := Input.EndsWith('play with');

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s