C++ Remove new line from multiline string

前端 未结 12 2375
暗喜
暗喜 2020-12-08 06:14

Whats the most efficient way of removing a \'newline\' from a std::string?

12条回答
  •  我在风中等你
    2020-12-08 06:39

     std::string some_str = SOME_VAL;
     if ( some_str.size() > 0 && some_str[some_str.length()-1] == '\n' ) 
      some_str.resize( some_str.length()-1 );
    

    or (removes several newlines at the end)

    some_str.resize( some_str.find_last_not_of(L"\n")+1 );
    

提交回复
热议问题