[C++] POCO Learning Summary (15): String Operations

[C++] Guo Laoer’s blog post: C++ directory

1 Introduction

POCO provides some functions to handle std::string and std::wstring;
For example:

  • Remove spaces
  • Case conversion
  • Case-insensitive comparison
  • Character conversion and substring replacement
  • connect

Many functions come in two variants:

  • A function that returns a new string without modifying the original string;
  • Function that directly modifies the original string (inPlace is appended to the function name);
  • All functions are in the Poco namespace.

2. Remove spaces

2.1 Description

  • std::[w]string trimLeft(const std::[w]string& str): Returns a copy of str, in the copyFrontThe spaces will be removed
  • std::[w]string& trimLeftInPlace(std::[w]string& str): Delete from strFrontof spaces and returns theQuote
  • std::[w]string trimRight(const std::[w]string& str): Returns a copy of str, in the copyendThe spaces will be removed
  • std::[w]string trimRightInPlace(std::[w]string& str): Delete from strendspace and return theQuote
  • Std::[w]string trim(const Std::[w]string& str): Returns a copy of str and deletes it from the copybefore and afterspace
  • std::[w]string trimInPlace(std::[w]string& str): Delete from strbefore and afterspace, and returns theQuote

2.2 Usage

#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;
}

3. Case conversion

3.1 Description

  • std::[w]string toUpper(const std::[w]string& str): Returns a copy of str with all characters converted tocapital
  • std::[w]string toLower(const std::[w]string& str): Returns a copy of str with all characters converted tolower case
  • std::[w]string& toUpperInPlace(std::[w]string& str): Convert all characters in str tocapital, and returns itsQuote
  • std::[w]string& toLowerInPlace(std::[w]string& str): Convert all characters in str tolower case, and returns itsQuote
  • int icompare(const std::[w]string& str1, const std::[w]string& str2): Case-insensitive comparison
    warn:These functions do not work with UTF-8 strings.

3.2 Usage

#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;
}

4. Replacement and splicing

4.1 Description

1) Replace characters

  • std::::[w]string translate(const std::[w]string& str, const std::[w]string& from, const std::[w]string& to)< /span>
    Returns a copy of str, with all characters in from replaced by the corresponding (positionally) characters in to. If there is no corresponding character in to, the character is deleted.
  • std::[w]string& translateInPlace(std::[w]string& str, const std::[w]string& from, const std::[w]string& to)
    Returns a modified str in which all characters in from are replaced by the corresponding (by position) characters in to. If there is no corresponding character in to, the character is deleted.

2) Substring replacement

  • std::[w]string replace(const std::[w]string& str, const std::[w]string& from, const std::[w]string& to)
    Returns a copy of str in which all occurrences of the substring given in from are replaced with the string given in to.
  • std::[w]string& replaceInPlace(std::[w]string& str, const std::[w]string& from, const std::[w]string& to)
    Returns a modified str in which all occurrences of the substring given in from are replaced with the string given in to.

3) Splicing

  • std::[w]string cat(const std::[w]string& s1, const std::[w]string& s2 [, const std::[w]string& s3 [,… ]])
    Concatenate up to six strings and return the result
  • template <class S, class It>
    S cat(const S& delimiter, const It& begin, const It& end)
    Concatenate all strings in the range [It, end], adding delimiter separator in the middle

4.2 Usage

#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;
}

5. Poco::NumberFormatter formats numbers into strings

Poco::NumberFormatter provides static methods to format numbers into strings, which is implemented internally using std::sprintf()

  • std::string format(int value): formatted as a decimal integer type string
  • std::string format(int value, int width): Format as a decimal integer type string, specify the minimum width width, if it is insufficient, fill it on the rightspace
  • std::string format0(int value, int width): Format as a decimal integer type string, specify the minimum width width, if insufficient, add the rightzero
  • std::string formatHex(int ​​value): formatted as a hexadecimal integer type string
  • std::string formatHex(int ​​value, int width): Format into a hexadecimal integer type string, specify the minimum width width, if it is insufficient, add it to the rightzero
  • std::string format(const void* ptr): Format the pointer ptr into an 8-bit (32-bit system) or 16-bit (64-bit system) string, in hexadecimal form
  • std::string format(double value): Format a floating-point string in decimal floating-point notation with eight decimal places of precision
  • std::string format(double value, int precision): Formatted as a decimal floating point number representing a floating point string with a precise number of decimal places
  • std::string format(double value, int width, int precision): A floating-point string formatted as a decimal float, right-justified in a field of the specified width, with the exact number of decimal places
#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;
}

6. Poco::NumberParser extracts numbers from strings

The static member functions of Poco::NumberParser class can be used to parse numbers from strings.

  • Int parse(const std::string&str): Parse the integer value in decimal form from str. If the string does not contain a valid number, an exception is thrown: SyntaxException.
  • bool tryParse(const std::string& str, int& value): Parse the decimal integer value from str and store it in value. Returns true if the number is valid. Otherwise returns false; in this case Value is undefined.
  • unsigned parseUnsigned(const std::string& str): parse unsigned
  • bool tryParseUnsigned(const std::string& str, unsigned& value)
  • unsigned parseHex(const std::string&str): parse hexadecimal
  • bool tryParseHex(const std::string& str, unsigned& value)
  • Int64 parse64(const std::string& str): Parse Int64
  • bool tryParse64(const std::string& str Int64& value)
  • UInt64 parseUnsigned64(const std::string&str): Parse unsigned UInt64
  • bool tryParseUnsigned64(const std::string& str UInt64& value)
  • UInt64 parseHex64(const std::string&str): Parse 64-bit hexadecimal
  • bool tryParseHex64(const std::string& str UInt64& value)
  • double parseFloat(const std::string&str): parse floating point type
  • bool tryParseFloat(const std::string& str, double& value)

7. Poco::StringTokenizer splits strings

#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;
}

8. Poco::RegularExpression regular expression

Use Poco::RegularExpression in POCO to implement regular expressions
Header file: #include “Poco/ regulareexpression.h”
Poco: : RegularExpression internally uses the PCRE library (Perl-compatible regular expressions).

Use regular expressions to match, extract, split, and replace substrings (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;
}

9. Poco::TextConverter character encoding conversion

Poco::TextConverter implements converting strings between different character encodings

#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;
}

10. Character conversion in stream

  • Poco::OutputStreamConverter: Acts as a filter that converts all characters written to it to another encoding before passing them to another stream.
  • Poco::InputStreamConverter: Acts as a filter that reads characters from another stream and converts them to another encoding before passing them to the reader.
#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;
}

11. Character encoding

Poco::Unicode: Provides basic support for Unicode character attributes (character category, character type, script)
Poco::UTF8: Processes UTF-8 encoded strings

Guess you like

Origin blog.csdn.net/u010168781/article/details/134983856