string.strip
Returns str with white-space characters removed from both ends.
Parameters
str | string | the string to trim |
Return value
A new string with the leading and trailing white space removed. If str is all white space, or empty, the result is an empty string.
Notes
White space here is exactly space, tab, newline, vertical tab, form feed and carriage return - the ASCII set the VM checks byte by byte. A non-ASCII byte, or any other control character, is not white space and is kept.
strip, lstrip, rstrip, split_by_chars, escape, startswith and endswith are also available as methods on every string, with the same behavior: " hi ".strip() gives the same result as strip(" hi ").
Example
from "string" import strip
println("strip(\" hello \") =", $"[{strip(" hello ")}]")
println("strip(\"\\t\\nhello\\r\\n\") =", $"[{strip("\t\nhello\r\n")}]")
// all white space strips to an empty string
println("strip(\" \") =", $"[{strip(" ")}]")
// bytes outside the white-space set are left alone
println("strip(\"--hi--\") =", $"[{strip("--hi--")}]")
// same operation, called as a method
println("\" hi \".strip() =", " hi ".strip())Output:
strip(" hello ") = [hello]
strip("\t\nhello\r\n") = [hello]
strip(" ") = []
strip("--hi--") = [--hi--]
" hi ".strip() = hiSee also
lstrip | Returns str with white-space characters removed from the beginning only. |
rstrip | Returns str with white-space characters removed from the end only. |
| string | module index |