trim() : Remove Whitespace
On this page
Summary
Remove leading and trailing whitespace from a string.
Signature
trim(str)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
str |
String | Yes | The string to trim |
Returns
Type: String
The string with leading and trailing whitespace removed.
Examples
Remove Extra Spaces
Data:
doc.Params["model"] = new {
text = " Hello World "
};
Output:
<p>"Hello World"</p>
Clean User Input
Data:
doc.Params["model"] = new {
names = new[] { " John ", " Jane ", "Bob " }
};
Output:
<li>John</li>
<li>Jane</li>
<li>Bob</li>
Normalize for Comparison
Data:
doc.Params["model"] = new {
status = " active "
};
Output:
<span class="active">Active</span>
Clean Multi-Line Text
Data:
doc.Params["model"] = new {
description = @"
This is a description
with extra whitespace
"
};
Output:
<p>This is a description
with extra whitespace</p>
Notes
- Removes spaces, tabs, newlines from both ends
- Does not affect whitespace in the middle of string
- Returns original string if no whitespace at ends
- For trailing whitespace only, use
trimEnd() - Does not modify the original value
- Useful for cleaning user input and data