POCO中的字串 文字和格式化 二

2021-06-28 00:55:26 字數 2098 閱讀 5622

從字串中提取數值

poco::numberparser類的靜態成員函式可以被用來從字串中解析數值

#include "poco/numberparser.h"

int parse(const std::string& str)

從字串中解析整數值。如果字串中沒有合法數字就丟擲syntaxexception。

bool tryparse(const std::string& str, int& value)

從字串中解析整數值並儲存到value中。如果成功返回true,否則返回false,value值不可用。

unsigned parseunsigned(const std::string& str)

bool tryparseunsigned(const std::string& str, unsigned& value)

unsigned parsehex(const std::string& str)

bool tryparsehex(const std::string& str, unsigned& value)

int64 parse64(const std::string& str)

bool tryparse64(const std::string& str int64& value)

uint64 parseunsigned64(const std::string& str)

bool tryparseunsigned64(const std::string& str uint64& value)

uint64 parsehex64(const std::string& str)

bool tryparsehex64(const std::string& str uint64& value)

double parsefloat(const std::string& str)

bool tryparsefloat(const std::string& str, double& value)

標記化字串

poco::stringtokenizer用來把字串分割為標記。

#include "poco/stringtokenizer.h"

poco::stringtokenizer建立時必須把字串、分隔符、操作標示作為傳遞引數。

poco::stringtokenizer內部容納了乙個包含提取標記的向量vector。

操作標示有:

tok_ignore_empty  空標記被忽略

tok_trim  刪除空格

#include "poco/stringtokenizer.h"

#include "poco/string.h" // for cat

using poco::stringtokenizer;

using poco::cat;

int main(int argc, char** argv)

正規表示式

通過poco::regularexpression類提供了支援正規表示式的能力。

#include "poco/regularexpression.h"

內部實現中,poco::regularexpression使用了pcre庫。

這意味著poco中的正規表示式與perl中的高度相容。

poco::regularexpression支援:

1、匹配符合正則的字串。

2、利用正規表示式提取子串。

3、利用正則替換子串。

不同的操作標示控制匹配行為。詳見手冊。

一些有用的操作標示:

re_caseless  執行大小寫敏感的匹配

re_anchored  把匹配模式視同以^開始

re_notempty  不匹配空字串

re_utf8  匹配模式和主題是utf-8編碼

#include "poco/regularexpression.h"

#include using poco::regularexpression;

int main(int argc, char** argv)

字串格式化

sprintf snprintf snprintf std stringstream std strstream boost lexical cast boost format cstring format 1 sprintf 使用 sprintf 不安全,輕則破壞資料的準確性,重則程式崩潰。請看下...

格式化字串

通常在使用字串的時候,會對字串進行格式化,然後輸出或呼叫 一般我們使用替換標記對字串進行格式化 string str1 string.format add is 1,2,3 而且在c 中的替換標記可以以任意順序和次數出現在格式化字串中,但替換值是按順序排的,而且替換標記不能超出索引範圍 string...

字串格式化

例如 string s hello map.put target world string res format s,map 有什麼用呢?比如在some.properties中配置模板字串,但是如果用 這種方式,在配置了spring讀取properties注入變數的時候,這個變數就找不到會報錯。這個...