Trim, Trim start, and Trim end in 4D

When working with user input, data cleaning, or text formatting, one of the most common tasks is removing unwanted whitespace. Whether it’s a space accidentally typed before an email address or a newline character left at the end of a copy-paste, these seemingly invisible characters can cause unexpected behavior in your applications.
Fortunately, 4D 21 provides easy-to-use string methods to help: Trim, Trim start, Trim end.

What is Trimming?

Trimming refers to the process of removing whitespace characters, including spaces, tabs, and line breaks, from a string. These characters are usually not visible but can affect string comparisons, storage, and formatting. This behavior follows the ECMAScript standard, which defines which characters are considered whitespace and how they should be removed.

Let’s look at the three key functions:

Trim: the all-around cleaner

Use Trim() when you want to remove all leading and trailing whitespace from a string.

var $cleaned := Trim("   hello world   ")
// $cleaned = "hello world"

Trim start: keep the tail

Trim start() only removes leading whitespace. The rest of the string remains unchanged.

var $cleaned := Trim start("   hello world   ")
// $cleaned = "hello world   "

Trim end: keep the head

And Trim end() does the opposite: it removes trailing whitespace but preserves the beginning of the string.

var $cleaned := Trim end("   hello world   ")
// $cleaned = "   hello world"

Try it in 4D!

Want to try it out? Just use Trim, Trim start, or Trim end with any text input in your 4D app and watch your strings get neat and tidy.

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.