Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Trim() function not working in ExtendScript. Is there a way to replicate the functioning?

Explorer ,
Oct 09, 2015 Oct 09, 2015

The error message says that trim() is not a function. I am applying this function to a string.

TOPICS
Scripting
8.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 09, 2015 Oct 09, 2015

Assuming that by trimming you mean deleting leading and trailing white space:

function trim (str) {

    return str.replace(/^\s+/,'').replace(/\s+$/,'');

}

Peter

Translate
Community Expert ,
Oct 09, 2015 Oct 09, 2015

Assuming that by trimming you mean deleting leading and trailing white space:

function trim (str) {

    return str.replace(/^\s+/,'').replace(/\s+$/,'');

}

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 20, 2018 May 20, 2018

Thank you for saving my day

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2015 Oct 09, 2015

Or if you want to use it in the form 'myString.trim()', define this at the beginning of your script:

String.prototype.trim = function () {

    return this.replace(/^\s+/,'').replace(/\s+$/,'');

}

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 09, 2015 Oct 09, 2015

Thanks a lot!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 02, 2016 May 02, 2016

pkahrel a écrit:

Or if you want to use it in the form 'myString.trim()', define this at the beginning of your script:

  1. String.prototype.trim=function(){
  2. returnthis.replace(/^\s+/,'').replace(/\s+$/,'');
  3. }

P.

Hi Peter,

It's generally better to check for the method availability especially for methods that could be possibly added natively in the future

if (Array.prototype.trim==null) Array.prototype.trim=function(){…

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
May 03, 2016 May 03, 2016

It's generally better to check for the method availability especially for methods that could be possibly added natively in the future

Loic, your optimism is admirable!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 30, 2016 Apr 30, 2016

Trim should also take care of new lines, carriage return, tabs, etc:

/**

* Trim spaces, new lines  etc

*

* @param {string} str

* @returns {string}

*/

function trim (str) {

  return str.replace(/(^[\s\n\r\t\x0B]+)|([\s\n\r\t\x0B]+$)/g, '');

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 12, 2017 Jun 12, 2017

What's left otherwise

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 11, 2017 Jul 11, 2017

So to consolidate advice/suggestions, something like this should be ideal for our goals right?

if (String.prototype.trim == null) String.prototype.trim = function () {return this.replace(/(^[\s\n\r\t\x0B]+)|([\s\n\r\t\x0B]+$)/g, '')};

Loic, I noticed you used Array.prototype instead of String.prototype.

Any particular reason? will giving Array a trim function also give it to String? Is there even a reason for arrays in general to use the trim function?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
May 21, 2018 May 21, 2018
LATEST

Wow sorry fo rnot responding to this one. I just missed it. In case it's still worth it. There wasn't any consideration about array over string. I think I just bounced on the topic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines