POCO提供一些函数来处理 std::string 和 std::wstring;
比如:
许多函数有两种变体:
#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;
}
#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); // 0
rc = icompare(hello, "Hello, Universe!"); // 1
return 0;
}
1)替换字符
2)子字符串替换
3)拼接
#include "Poco/String.h"
#include <vector>
using Poco::translateInPlace;
using Poco::replace;
using Poco::cat;
int main(int argc, char** argv)
{
std::string s1("Eiffel Tower");
translateInPlace(s1, "Eelo", "3310"); // "3iff31 T0w3r"
std::string s2("aabbcc");
std::string r(replace(s2, "aa", "AA")); // "AAbbcc"
r = replace(s2, "bb", "xxx"); // "aaxxxcc"
r = replace(s2, "bbcc", ""); // "aa"
std::vector<std::string> colors;
colors.push_back("red");
colors.push_back("green");
colors.push_back("blue");
std::string s3;
s3 = cat(std::string(", "), colors.begin(), colors.end()); //"red, green, blue"
return 0;
}
Poco::NumberFormatter提供静态方法将数字格式化为字符串,其内部为使用std::sprintf()来实现
#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::NumberParser类的静态成员函数可用于从字符串中解析数字。
#include "Poco/StringTokenizer.h"
#include "Poco/String.h" // for cat
using Poco::StringTokenizer;
using Poco::cat;
int main(int argc, char** argv)
{
StringTokenizer t1("red, green, blue", ",");// "red", " green", " blue" (note the whitespace)
StringTokenizer t2("red,green,,blue", ",");// "red", "green", "", "blue"
StringTokenizer t3("red; green, blue", ",;",StringTokenizer::TOK_TRIM);// "red", "green", "blue"
StringTokenizer t4("red; green,, blue", ",;",StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);// "red", "green", "blue"
std::string s(cat(std::string("; "), t4.begin(), t4.end()));// "red; green; blue"
return 0;
}
在POCO中使用 Poco::RegularExpression 来实现正则表达式
头文件: #include “Poco/ regulareexpression .h”
Poco:: RegularExpression 在内部使用PCRE库(Perl兼容正则表达式)。
用正则表达式匹配(match)、提取(extract)、分割(split)、替换子字符串(subst)
#include "Poco/RegularExpression.h"
#include <vector>
using Poco::RegularExpression;
int main(int argc, char** argv)
{
RegularExpression re1("[0-9]+");
bool match = re1.match("123"); // true
match = re1.match("abc"); // false
match = re1.match("abc123", 3); // true
RegularExpression::Match pos;
re1.match("123", 0, pos); // pos.offset == 0, pos.length == 3
re1.match("ab12de", 0, pos); // pos.offset == 2, pos.length == 2
re1.match("abcd", 0, pos); // pos.offset == std::string::npos
RegularExpression re2("([0-9]+) ([0-9]+)");
RegularExpression::MatchVec posVec;
re2.match("123 456", 0, posVec);
// posVec[0].offset == 0, posVec[0].length == 7
// posVec[1].offset == 0, posVec[1].length == 3
// posVec[2].offset == 4, posVec[2].length == 3
std::string s;
int n = re1.extract("123", s); // n == 1, s == "123"
n = re1.extract("ab12de", 0, s); // n == 1, s == "12"
n = re1.extract("abcd", 0, s); // n == 0, s == ""
std::vector<std::string> vec;
re2.split("123 456", 0, vec);
// vec[0] == "123"
// vec[1] == "456"
s = "123";
re1.subst(s, "ABC"); // s == "ABC"
s = "123 456";
re2.subst(s, "$2 $1"); // s == "456 123"
RegularExpression re3("ABC");
RegularExpression re4("ABC", RegularExpression::RE_CASELESS);
match = re3.match("abc", 0); // false
match = re4.match("abc", 0); // true
return 0;
}
Poco::TextConverter 实现在不同字符编码之间转换字符串
#include "Poco/TextConverter.h"
#include "Poco/Latin1Encoding.h"
#include "Poco/UTF8Encoding.h"
#include <iostream>
using Poco::TextConverter;
using Poco::Latin1Encoding;
using Poco::UTF8Encoding;
int main(int argc, char** argv)
{
std::string latin1String("This is Latin-1 encoded text.");
std::string utf8String;
Latin1Encoding latin1;
UTF8Encoding utf8;
TextConverter converter(latin1, utf8);
converter.convert(latin1String, utf8String);
std::cout << utf8String << std::endl;
return 0;
}
#include "Poco/StreamConverter.h"
#include "Poco/Latin1Encoding.h"
#include "Poco/UTF8Encoding.h"
#include <iostream>
using Poco::OutputStreamConverter;
using Poco::Latin1Encoding;
using Poco::UTF8Encoding;
int main(int argc, char** argv)
{
std::string latin1String("This is Latin-1 encoded text.");
Latin1Encoding latin1;
UTF8Encoding utf8;
OutputStreamConverter converter(std::cout, latin1, utf8);
converter << latin1String << std::endl; // console output will be UTF-8
return 0;
}
Poco::Unicode:提供了对Unicode字符属性(字符类别、字符类型、脚本)的基本支持
Poco::UTF8:处理UTF-8编码的字符串