Javascript & Python: on the importance of using trim() for text-trimming

Trim is a function which is in literal-conversion known in Javascript, C# PHP, Java, R, etc..
In python the function is called strip() as a member of the Object String.
Its purpose it to cut the whitespace characters at the beginning of a string or text, with definitions and additional parameters differing slightly from language to language.

The following example neatly shows its use in Javascript. The site used is offline available in the Chrome > 14 installation. It is the default view when no site is selected. 


Fire up the web developer inspector ( STRG+SHIFT+I) , click the tab Console and enter the following text and then press RETURN to interpret the javascript.


Here is above's example as text (> denotes the console command line):

>document.getElementById('attribution')


>document.getElementById('attribution').innerHTML
"
Theme created by

"
>document.getElementById('attribution').innerText
"
Theme created by

"
>document.getElementById('attribution').innerText.split(" ")
["
", "", "", "", "", "", "Theme", "created", "by
", "", "", "", "", "", "
", "", "", "", ""]

>document.getElementById('attribution').innerText.trim().split(" ")
["Theme", "created", "by"]
In python the command is executed analogously, but by using strip instead. Additionally by passing a character or string these set of characters is removed at the beginning till the first character outside not included in the set

>" Hello te st ".strip()
'Hello test'
>'www.blogger.com'.strip('comwzu.')
'blogger'

LihatTutupKomentar