7 With strings attached - working with string literals

 

This chapter covers

  • Understanding the concept of strings and how they differ from character arrays
  • Declaring and initializing string literals, working with std::string
  • Using std::string methods to find, insert, replace, remove elements from a string and more
  • Trimming and formatting strings
  • Using string comparison using three-way comparison and other methods
  • Using wstring multi-lingual input

In this chapter, you will expand your skillset by learning about strings, a commonly used data type in C++ that represents a sequence of characters. You will understand what strings are and when they are used (spoiler: in almost any program). You will also learn about various std::string methods for manipulating, searching, inserting, replacing, and removing elements from strings. We will dive deep into string trimming, which is a way to get rid of leading and trailing whitespace characters from a string. We will also explore in greater detail how to use the three-way comparison operator (also known as the spaceship operator) to compare strings, as you learned about in Chapter 3.

7.1 Pulling strings – The importance of string literals

7.1.1 String Vs. an array of chars

7.1.2 The Brick and the Wall: compound Vs fundamental Types

7.2 Declaring and initializing strings

7.2.1 Declaring and initializing a string variable

7.2.2 Sting input: Why using getline() is far better than ‘cin’

7.3 Playing the strings – string boosting methods

7.3.1 Copying strings value

7.3.2 Concatenating strings

7.3.3 Keep it short: using erase() to shorten strings

7.3.4 Using the substr() function for obtaining sub-strings

7.4 Search and you’ll find() –string searching methods

7.4.1 “hcraes sgnirts” - Searching strings in reverse

7.4.2 Every ‘starts’ has an ‘ends’

7.5 “Triminology” – introduction to string trimming

7.5.1 Give me space – handling multiple spaces between characters

7.5.2 Trimming, C++ and I

7.6 The whole string of things - more strings manipulations

7.6.1 Replacing strings

7.7 Formatting strings

7.8 It’s a small world: handling multilingual programs

7.9 Strings comparison – the old way and the spaceship way

7.9.1 To string or not to string – that is the question

7.10 Summary

sitemap