[Poco study notes] Common functions of String library

table of Contents

Preface

1. Trimming

Two, case conversion

Three, translation function

Four, string replacement

Five, string comparison

Six, string splicing

to sum up


Preface

The Poco library encapsulates a corresponding String processing library based on the C++ string library and wstring library. Poco's string library can be used to implement string formatting, regular expressions, string encoding, and so on.

Introduce below, the Poco library provides us with some commonly used string formatting operations, such as: string trimming, string conversion, string case-insensitive comparison, string replacement, etc.

The functional functions in Poco's string library The same functional function will be divided into two categories

  • Return the value of the string after processing, without modifying the original string
  • Modify the original string

Poco's library functions use InPlace to distinguish these two types of functions, that is, to achieve the same function. If the function name has InPlace, it means that the original string is modified.

Let's start to learn a few functional functions

1. Trimming

trim is a function provided by poco to delete the beginning and ending blanks of a string

//trim 是poco提供的一个删除字符串开头跟结尾空白的函数
//函数带Inplace的是修改字符串本身,不带Inplace的是返回修改的字符串的值。
void LearnTrim()
{
	using Poco::trim;
	using Poco::trimLeft;
	using Poco::trimRight;
	using Poco::trimRightInPlace;

	std::string hello("    Hello,    baiyu!   ");
	std::cout << "original  string: " << hello << "666" << std::endl;//"    Hello,    baiyu!   666"

	//删除字符串开头的空格
	std::string s1(trimLeft(hello)); // "Hello,    baiyu!   666"
	std::cout << "trimLeft  string: " << s1 << "666" << std::endl;

	//删除字符串结尾的空格
	trimRightInPlace(s1); // "Hello,    baiyu!666"
	std::cout << "trimRightInPlace: " << s1 << "666" << std::endl;

	//删除字符串开头跟结尾的所有空格
	std::string s2(trim(hello)); // "Hello,    baiyu!666"
	std::cout << "trim      string: " << s2 << "666" << std::endl;
}

Two, case conversion

//大小写转换函数
void LearnConversion()
{
	using Poco::toUpper;
	using Poco::toLower;
	using Poco::toLowerInPlace;
	using Poco::icompare;
	std::string s("Hello Baiyu");
	std::cout << "original string: " << s << std::endl;

	std::string s1(toUpper(s));//HELLO BAIYU
	std::cout << "s1 string: " << s1 << std::endl;

	std::string s2(toLower(s));//hello baiyu
	std::cout << "s2 string: " << s2 << std::endl;
}

Three, translation function

void LearnTranslate()
{
	using Poco::translateInPlace;
	std::string s("Hello Baiyu!");

	std::cout <<"original: "<< s << std::endl;

	//按顺序翻译替换对应的位置的字符
	//H-->*   e-->5   y-->2   u-->5   !-->0   1-->?
	translateInPlace(s, "Heyu!l", "*5250?"); // "*5??o Bai250"

	std::cout << "transerlate: " << s << std::endl;
}

Four, string replacement

void LearnSubstringReplace()
{
	using Poco::replace;
	std::string s("baiyu aaccbb");//baiyu aaccbb
	std::cout << s << std::endl;

	std::string r(replace(s, "aa", "AA"));//baiyu AAccbb
	std::cout << r << std::endl;

	r = replace(s, "baiyu", "zhh");//zhh aaccbb
	std::cout << r << std::endl;
}

Five, string comparison

void LearnComparison()
{
	// icompare的结果如下:
	//	0 if str1 == str2
	// -1 if str1 < str2
	// +1 if str1 > str2

	using Poco::icompare;
	std::string s1("baiyu");
	std::string s2("Baiyu");
	std::string s3("hello world");
	std::string s4("C");
	std::string s5("B");

	int rc = icompare(s1, s2);
	std::cout << rc << std::endl;

	rc = icompare(s1, "zhh");
	std::cout << rc << std::endl;

	rc = icompare(s4, s5);
	std::cout << rc << std::endl;

}

Six, string splicing

void LearnCat()
{
	using Poco::cat;
	std::string s;
	std::string s1("baiyu, ");
	std::string s2("hello world");

	s = cat(s1, s2);//直接字符串拼接
	std::cout << s << std::endl;

	std::vector<std::string> names;
	names.push_back("baiyu");
	names.push_back("zhh");
	names.push_back("man");
	//遍历字符串数据拼接,delim是连接符
	s = cat(std::string(", "), names.begin(), names.end());
	// "baiyu, zhh, man"
	std::cout << s << std::endl;	
}

to sum up

The string function encapsulated by the Poco library is relatively easy to use~

For more specific use of the String function of the Poco library, refer to the header file Poco/String.h

Guess you like

Origin blog.csdn.net/zhh763984017/article/details/114242126