POCO中的字符串、文本和格式化

来源:互联网 发布:淘宝14a中空铝隔条价格 编辑:程序博客网 时间:2024/05/26 22:07

概要:字符串函数、格式化和解析数字、标记化字符串、正则表达式、文本编码


字符串函数
对于常用的字符串操作POCO提供了大量函数模板:
trimming(消除空白) 、大小写转换、大小写敏感的比较
、字符翻译和子串替换、字符串连接
#include "Poco/String.h"
这些函数兼容std::string和std::wstring
许多函数都有两个版本:一个保持原字符串不变返回一个新的字符串;一个直接修改原字符串。
后一版本被称为“in place”函数,在函数名称后面都有inPlace后缀。
所有函数都在Poco名称空间下。
Trimming函数
std::[w]string trimLeft(const std::[w]string& str)
返回消除了左侧空格的字符串拷贝
std::[w]string& trimLeftInPlace(std::[w]string& str)
返回消除了左侧空格的字符串引用
std::[w]string trimRight(const std::[w]string& str)
返回消除了右侧空格的字符串拷贝
std::[w]string trimRightInPlace(std::[w]string& str)
返回消除了右侧空格的字符串引用
std::[w]string trim(const std::[w]string& str)
返回消除了所有空格的字符串拷贝
std::[w]string trimInPlace(std::[w]string& str)
返回消除了所有空格的字符串引用


#include <Poco/String.h>using Poco::trim;using Poco::trimLeft;using Poco::trimRight;using Poco::trimRightInPlace;int main(int argc, char** argv){std::string hello(" Hello, world! ");std::string s1(trimLeft(hello)); // "Hello, world! "trimRightInPlace(s1);// "Hello, world!"std::string s2(trim(hello));// "Hello, world!"return 0;}


std::[w]string toUpper(const std::[w]string& str)
std::[w]string toLower(const std::[w]string& str)
返回转换为大写/小写的字符串拷贝
std::[w]string& toUpperInPlace(std::[w]string& str)
std::[w]string& toLowerInPlace(std::[w]string& str)
返回转换为大写/小写的字符串引用
注意:这些函数不适用于UTF-8字符串。UTF-8的替换请参见Poco::UTF8。
int icompare(const std::[w]string& str1, const std::[w]string& str2)
比较str1 和 str2,如果str1 == str2 返回0,str1 > str2 返回1, str1 < str2 返回 -1。
有大量的变量替换子串,迭代器,c风格字符串等类型的函数,详见参考手册。
注意:这些函数不适用于UTF-8字符串。使用Poco::UTF8String。
#include "Poco/String.h"using Poco::toUpper;using Poco::toLower;using Poco::toLowerInPlace;using Poco::icompare;int main(int argc, char** argv){std::string hello("Hello, world!");std::string s1(toUpper(hello)); // "HELLO, WORLD!"toLowerInPlace(s1);// "hello, world!"int rc = icompare(hello, s1);// 0rc = icompare(hello, "Hello, Universe!"); // 1return 0;}


在内部,大小写转换和大小写敏感比较函数使用了C++Locales。
对ASCII字符一直有效,对于非ASCII字符是否有效取决于特定的C++
库实现(和本地化设置)
要实现可靠地多语言大小写转换,最好使用第三方库,比如ICU。


字符翻译
std::[w]string translate(const std::[w]string& str,
const std::[w]string& from, const std::[w]string& to)
用“to”中的字符相应替换“from”中的字符,并返回字符串拷贝。
std::[w]string& translateInPlace(std::[w]string& str,
const std::[w]string& from, const std::[w]string& to)
“from”“to”也可以是C风格的字符串。
#include "Poco/String.h"using Poco::translateInPlace;int main(int argc, char** argv){std::string s("Eiffel Tower");translateInPlace(s, "Eelo", "3310"); // "3iff31 T0w3r"return 0;}


std::[w]string replace(const std::[w]string& str,const std::[w]string& from, const std::[w]string& to)
用“to”字符串替换所有“from”字符串,并返回字符串拷贝。
std::[w]string& replaceInPlace(std::[w]string& str,const std::[w]string& from, const std::[w]string& to)
“from”“to”也可以是C风格的字符串。
#include "Poco/String.h"using Poco::replace;int main(int argc, char** argv){std::string s("aabbcc");std::string r(replace(s, "aa", "AA")); // "AAbbcc"r = replace(s, "bb", "xxx");// "aaxxxcc"r = replace(s, "bbcc", "");// "aa"return 0;}


std::[w]string cat(const std::[w]string& s1,const std::[w]string& s2 [, const std::[w]string& s3 [,...]])
连接最多6个字符串并返回结果。
template <class S, class It>
S cat(const S& delimiter, const It& begin, const It& end)
连接给定开始和结束区域并且由分隔符隔开的的字符串
cat()函数比std::string的+操作效率更高。

#include "Poco/String.h"#include <vector>using Poco::cat;int main(int argc, char** argv){std::vector<std::string> colors;colors.push_back("red");colors.push_back("green");colors.push_back("blue");std::string s;s = cat(std::string(", "), colors.begin(), colors.end());// "red, green, blue"return 0;}



数值格式化


Poco::NumberFormatter提供了把数值格式化成字符串的静态方法
#include "Poco/NumberFormatter.h"
所有的方法对于int, unsigned int, long,unsigned long, Int64 and UInt64都适用。
内部实现中方法用到了std::sprintf()。
std::string format(int value)
用尽可能小的空间把整数值格式化成10进制的标记。
std::string format(int value, int width)
把整数值格式化成10进制的标记,位数至少为width。
std::string format0(int value, int width)
把整数值格式化成10进制的标记,位数至少为width,前缀补0。
std::string formatHex(int value)
把整数值格式化成16进制的标记字符串。
std::string formatHex(int value, int width)
把整数值格式化成16进制的标记字符串,,位数至少为width,前缀补0。
std::string format(const void* ptr)
把指针类型格式化成8位(32位指针)或16位(64位指针)宽度16进制表示字符串
同理还有
std::string formatHex(int value)
std::string formatHex(int value, int width)
std::string format(const void* ptr)
std::string format(double value)
std::string format(double value, int precision)
std::string format(double value, int width, int precision)
所有的format()成员函数都有相应的append()可以将数值附加到已有的字符串上。
使用append()能大大提高效率。
例如void append(std::string& str, int value);
format()使用append()来实现。
注意:转换的准确结果取决于当前的区域设置。


#include "Poco/NumberFormatter.h"using Poco::NumberFormatter;int main(int argc, char** argv){std::string s;s = NumberFormatter::format(123);    //“123”s = NumberFormatter::format(123, 5); //“  123”s = NumberFormatter::format(-123, 5);//“ -123”s = NumberFormatter::format(12345, 3);//“12345”s = NumberFormatter::format0(123, 5); //“00123”s = NumberFormatter::formatHex(123);// "7B"s = NumberFormatter::formatHex(123, 4); // "007B"s = NumberFormatter::format(1.5);// "1.5"s = NumberFormatter::format(1.5, 2);// "1.50"s = NumberFormatter::format(1.5, 5, 2); // " 1.50"s = NumberFormatter::format(&s);// "00235F7D"return 0;}



类型安全的打印风格的格式化
POCO提供了类似sprintf的格式化函数,但是可以应用于std::string,并且是类型安全的。
#include "Poco/Format.h"
std::string format(const std::string& format,const Any& value1[, const Any& value2[, ...]])
void format(std::string& result, const std::string& format,const Any& value1[, const Any& value2[, ...]])


这个格式化功能与printf()高度兼容,但两者不同。
最多支持6个参数。
如果数值不匹配特定格式会导致ERRFMT错误。
如果数值比格式参数多,多余的数值被简单忽略。
#include "Poco/Format.h"using Poco::format;int main(int argc, char** argv){int n = 42;std::string s;format(s, "The answer to life, the universe and everything is %d", n);s = format("%d + %d = %d", 2, 2, 4); // "2 + 2 = 4"s = format("%4d", 42);// " 42"s = format("%-4d", 42);// "42 "format(s, "%d", std::string("foo")); // "[ERRFMT]"return 0;}




0 0
原创粉丝点击