[topic] 末尾の空白文字を取り除く

与えられた文字列の末尾の空白文字を取り除く方法と、その操作が与えられた文字列を破壊するかどうか。取り除かれる空白文字の種類。

Posted feedbacks - C++

boost を使えば、boost::algorithm に trim が用意されています。trim_right_copy が非破壊で、trim_right が破壊的です。削除される文字は isspace が true になるものと同じです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
#include "boost/algorithm/string/trim.hpp"

using namespace std;
using namespace boost::algorithm;

int main ( int argc, char *argv[] ){
   string str1( argv[1] );
   string str2 = trim_right_copy( str1 );
   cout << str1 << "#" << endl;
   cout << str2 << "#" << endl;
   trim_right( str1 );
   cout << str1 << "#" << endl;
}

Index

Feed

Other

Link

Pathtraq

loading...