修剪空白
要從字串的邊緣修剪空白,請使用 String.prototype.trim
:
" some whitespaced string ".trim(); // "some whitespaced string"
許多 JavaScript 引擎,但不是 Internet Explorer ,已經實現了非標準的 trimLeft
和 trimRight
方法。對於標準化的 trimStart
和 trimEnd
方法,目前處於該過程的第 1 階段的提議 ,為了相容性而別名為 trimLeft
和 trimRight
。
// Stage 1 proposal
" this is me ".trimStart(); // "this is me "
" this is me ".trimEnd(); // " this is me"
// Non-standard methods, but currently implemented by most engines
" this is me ".trimLeft(); // "this is me "
" this is me ".trimRight(); // " this is me"