Page 1 of 1

leading and trailing spaces: trim

Posted: Wed May 27, 2015 2:02 pm
by ArielRauch
Hi,
I somehow forgot how to trim a string.
str.trim() does not work.

Thank you in advanced,

Ariel

Re: leading and trailing spaces: trim

Posted: Wed May 27, 2015 3:22 pm
by loicaigon
Hi,

No trim function in Switch Javascript Object Model but it's pretty easy to get this result with a replace call:

Code: Select all

var str = "      foo        "; 
">"+str.replace(/(^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$)/g, "" )+"<" //>foo<
HTH
Loic

Re: leading and trailing spaces: trim

Posted: Wed May 27, 2015 3:46 pm
by gabrielp
loicaigon wrote:No trim function in Switch Javascript Object Model but it's pretty easy to get this result with a replace call:
Thanks for sharing this. That is very helpful.

Re: leading and trailing spaces: trim

Posted: Wed May 27, 2015 5:11 pm
by ArielRauch
I agree with Gabriel - very helpful.

Thank you very much

Ariel

Re: leading and trailing spaces: trim

Posted: Wed May 27, 2015 5:26 pm
by loicaigon
My pleasure. Be aware though that it will only remove trailing and leading "spaces". Something like "hey you" won't be considered with previous regular expression but I was sticking to the idea of trimming the string.
For the ones looking for a more global replacement like " he llo " to "hello", you may prefer to use :

Code: Select all

str.replace (/[\s\uFEFF\xA0]+/g, "")

Re: leading and trailing spaces: trim

Posted: Mon Jun 15, 2015 5:20 pm
by andrea mahoney
You can use a Switch Variable to do this with norm or trim from the Variable options:

job.getVariableAsString("[Job.NameProper:Space=\"norm\"]");