Sidechair

str.trim()

Browse the reference

Returns a string with spaces, tabs and line breaks removed from both ends.

Try it in the editor

str.trim strips leading and trailing white space. Spaces inside the text stay.

Apply it to user-entered text before comparing or converting it, because a stray space makes str.tonumber give na.

Syntax

Pine Script
str.trim(source: const string) -> const string
str.trim(source: input string) -> input string
str.trim(source: simple string) -> simple string
str.trim(source: series string) -> series string

Arguments

NameTypeDefaultDescription
sourceconst/input/simple/series stringrequiredThe text to clean up. Spaces and line breaks at both ends are removed.

Returns

const/input/simple/series string: the text without white space at either end.

Remarks

  • A string of only spaces becomes an empty string.

Example

Pine ScriptTrim before parsingOpen in editor
//@version=6
indicator("Trim")
txt = input.string(" 42 ", "Number")
plot(str.tonumber(str.trim(txt)), "Parsed", color = color.blue, style = plot.style_stepline)

See also

Runs on Sidechair