JavaScript

StringtrimChars Method

Syntax

String.trimChars([part[,characters])

Arguments

partstring

The part of the string to trim.

charactersstring

The characters to trim.

Returns

valuestring

The value of the string with the characters removed.

Description

Extension to the native string variable to trim characters from the start and/or end of the string.

Discussion

The String.trimChars method can be used to trim characters from the start and/or end of a string.

The "part" argument can be "start" or "left" to trim from the start of the string, or "end" or "right" to trim from the end of the string. A value of "all" can be used to trim from both sides of the string.

Example

var str = '   hello -  ';
var strTrim = str.trimChars('all');
// strTrim = 'hello -'
strTrim = str.trimChars('end',' -');
// strTrim = '   hello'