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

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

Explorer ,
Oct 09, 2015 Oct 09, 2015

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

7.5K

Translate

Translate

Report

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

Votes

Translate

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

Copy link to clipboard

Copied

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

function trim (str) {

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

}

Peter

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thank you for saving my day

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thanks a lot!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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, '');

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

What's left otherwise

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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