std::string
¶
Initialization¶
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, initialization)
{
std::string hello("Hello"); // <--- "const char*" ctor
std::string another_hello = hello; // <--- copy ctor
std::string who = "Joerg"; // <--- "const char*" ctor (assignment style)
}
Convenient Operators: Addition, Comparison, And Such¶
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, addition)
{
std::string hello = "Hello";
std::string who = "Joerg";
std::string greeting1 = hello + " " + who; // <--- inefficient
ASSERT_EQ(greeting1, "Hello Joerg");
std::string greeting2 = hello;
greeting2 += ' '; // <--- a little better
greeting2 += who; // <--- a little better
}
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, comparison)
{
std::string aaron = "Aaron";
std::string adam = "Adam";
// "Aaron" compares less-than "Adam", lexicographically
if (aaron < adam) {
// yes
}
else {
FAIL(); // not reached because aaron < adam
}
}
Conversion To Numbers: std::sto*()
¶
In namesapce std
, there exist several functions to convert strings
to numbers.
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, convert_to_signed_integer)
{
{
std::string numstr = "42";
int answer = std::stoi(numstr);
ASSERT_EQ(answer, 42);
}
{
std::string numstr = "42";
int answer = std::stoi(numstr, /*start pos*/0, /*base*/16);
ASSERT_EQ(answer, 66);
}
}
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, convert_to_unsigned_integer)
{
{
std::string numstr = "42";
unsigned long answer = std::stoul(numstr);
ASSERT_EQ(answer, 42);
}
{
std::string numstr = "-42";
unsigned long answer = std::stoul(numstr); // <--- for sure not negative
(void)answer;
}
}
If the string does not contain what is expected, the conversion
functions throw an instance of std::invalid_argument
.
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, convert_to_number_error)
{
try {
std::stoi("not-a-valid-number");
FAIL(); // <--- not reached because stoi throws
}
catch (const std::invalid_argument&) {}
}
Conversion To String: std::to_string()
¶
In namespace std
, there exist several overloads to convert any
kind of numbers to a string.
std::string to_string(int value);
std::string to_string(long value);
std::string to_string(long long value);
std::string to_string(unsigned value);
std::string to_string(unsigned long value);
std::string to_string(unsigned long long value);
std::string to_string(float value);
std::string to_string(double value);
std::string to_string(long double value);
// bring table:
// https://en.cppreference.com/w/cpp/string/basic_string/to_string
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, convert_from_something)
{
int i = 42;
std::string i_str = std::to_string(i);
ASSERT_EQ(i_str, "42");
unsigned int ui = 42;
std::string ui_str = std::to_string(ui);
ASSERT_EQ(ui_str, "42");
double d = 42.666;
std::string d_str = std::to_string(d);
ASSERT_EQ(d_str, "42.666000"); // sprintf default precision is 6
}
Searching: s.find()
¶
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, find)
{
std::string s = "Mississippi";
size_t pos = s.find("ss");
ASSERT_EQ(pos, 2);
pos = s.find('s');
ASSERT_EQ(pos, 2);
pos = s.find("ss", 3);
ASSERT_EQ(pos, 5);
}
Substrings: s.substr()
¶
#include <gtest/gtest.h>
#include <string>
TEST(string_suite, substr)
{
std::string s = "Mississippi";
std::string is = s.substr(1, /*substring length:*/2);
ASSERT_EQ(is, "is");
}