2

I know how to remove one or many spaces before the first word in a string in C, but I don't know in C++ (if there is a function by example).

My string is : " Hello" and I want to get "Hello". How can I do ?

Meugiwara
  • 599
  • 1
  • 7
  • 13

1 Answers1

2

Use std::string::find_first_not_of(' ') to get the index of the first non-whitespace character, then take the substring from there

Example:

std::string str = " Hello";
auto pos = str.find_first_not_of(' ');
auto Trimmed = str.substr(pos != std::string::npos ? pos : 0);

std::string TrimLeft(const std::string& str){
    auto pos = str.find_first_not_of(' ');
    return str.substr(pos != std::string::npos ? pos : 0);
}
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68