I'm sorry to answer with a solution involving JavaScript (JS); I know that's an impediment for a lot of people, and I hope someone responds with something that doesn't involve JS.
But anyway, JS has a method of the String object that I think will do just what you need. It's called trim, and it's trivial to use. It just returns a new string with the white space taken off of the front & back. With the exception that it is JS, it is truly trivial.
First, the value of a Text field in Memento is a String object, so that's a natural thing. Second, a method is merely a function that works on an object, like a String object, so that's kind of natural, too.
Here's how ya use it...
Since the value of a String field, like a Text field or a Barcode field, is a String object, you can just say field().trim() in your script code. See the attached screenshot for the Mozilla Developer Network (MDN) documentation of trim(). Mozilla is the organization that owns & oversees JS.
So, usually the simplest way to do JS in Memento is to use a JavaScript field. So, if you add a JavaScript field in Edit Library and give it the script...
field("Your field name here").trim(); // Doing that will cause the value of the JS field to be the value of the untrimmed field trimmed.
If you prefer a trigger or action script, you'll need to identify which entry is the current one, but otherwise, it's the same. In a trigger or action script, the current entry is provided by the entry() function, so in this case, you'll not add a JS field, but instead, you'll either change the value of the untrimmed field to its trimmed value, or you'll add a clone field (identical to the untrimmed field) to hold the trimmed value. This clone field, since it is a clone, can then act just like the original field in all ways; it's a duplicate. In this case, the trimming is done once to the clone field and does not affect the original field in any way. The code will be ...
entry().set ("Your field name here", entry().field("Your field name here").trim(); // Changes the field in place. Considerably more noisy, but still trivial.
or, for a clone...
entry().set ("Your clone field's name here", entry().field("Your field name here").trim(); // Makes a trimmed copy of your original field. Considerably more noisy, but still trivial.
Good luck. Let me know if I can be of further assistance.