Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #374 Trim whitespace

Create the string t consisting of the string s without its leading and trailing whitespace characters.

t = s.strip
#include <string>
using namespace std;
string t {s};
int n (t.length()), x {}, y {n - 1};
while (x not_eq  n && isspace(t[x])) ++x;
while (y not_eq -1 && isspace(t[y])) --y;
t = t.substr(x, (y - x) + 1);
import "strings"
t := strings.TrimSpace(s)
let t = s.trim()
String t = s.trim();
String t = s.strip();
t = s.strip()
from string import whitespace
t = s.strip(whitespace)

New implementation...
< >
programming-idioms.org