[boost]字串trim與數值轉換

posted in: boost, C/C++程式設計 | 0

用下面的方法,可以把一個字串多餘的部份拿掉並將其轉成浮點數:(沒去掉後面的1的話會產生run time錯誤)

string data = "           abc: 3.40    1         "
boost::trim(data);//把前面跟後面的空白去掉
boost::trim_if(data, boost::is_any_of("abc: "));//拿掉abc:
boost::algorithm::trim_right_if(data, is_any_of(" 1"));//拿掉後面的1
float value = boost::lexical_cast(data);

執行結果:3.4

Leave a Reply

Your email address will not be published. Required fields are marked *