University of Leeds

Strings

Introduction

You should already be comfortable in using the string class and be able to create and use strings to store text data (usually from user-input cin). The C++ string class is an improvement on the C-like strings which are essentially character arrays. It contains several methods for manipulating and creating strings.

It can be used by including the following header.

#include <string>

Note that C-like strings can be manipulated using the cstring functions.

Conversion

Several methods exist to convert strings to numeric data types as well as converting numeric data types to strings.

  // create a string
  std::string my_string = "1";
  // then conver to an int
  int my_int = std::stoi(my_string);
  std::cout << "My int is " << my_int << std::endl;
  // do similar for a double
  std::string my_new_string = "1.23456789";
  double my_double = std::stod(my_new_string);
  std::cout << "My double is " << my_double << std::endl;

  // we can also go to other way
  int my_new_int = 6;
  std::string another_new_string = std::to_string(my_new_int);

  // to string also works with doubles
  double my_new_double = 9.999999;
  std::string yet_another_new_string = std::to_string(my_new_double);

Concatentating

Using the string class, it is trivial to concatenate (join) strings using the + operator.

  // we easily concatenate strings using the string class
  std::string forename = "Craig";
  std::string surname = "Evans";
  std::string fullname = forename + " " + surname;
  std::cout << "My name is " << fullname << std::endl;

Thids gives the following output.

My name is Craig Evans

Other methods

There are several useful methods for extracting characters and getting to size of a string. note that is also possible to access specific characters using array like syntax e.g. str[4].

  // we can also do some more interesting stuff
  char first_initial = forename.front();
  char second_initial = surname.front();
  // front gets the first character of string
  // you can probably guess what back() does
  // we could also treat it like an array i.e. forename[0]
  std::cout << "My initials are " << first_initial << second_initial
            << std::endl;

  std::cout << "My surname has " << surname.size() << " letters.\n";

This will produce the following output.

My initials are CE
My surname has 5 letters.

Inserting substrings

We can also insert substrings into existing strings.

// we can insert a substring into a string
  std::string middle_initial = "A. ";
  fullname.insert(6, middle_initial);
  std::cout << "My full name is " << fullname << std::endl;

This will produce the following output.

My full name is Craig A. Evans

Finding sub-strings

It is also possible to search a string to see whether it contains a specific character or sub-string. This example demonstrates how to split a CSV string (i.e. look for a comma).

// create a csv string
  std::string csv = "Evans,87";

  // find the position of the comma
  std::size_t pos = csv.find(',');
  std::string name;
  int score;
  // if the string contains a comma
  if (pos != std::string::npos) {
    // the name is from the beginning (0) to the comma (pos)
    name = csv.substr(0, pos);
    // the score is after the comma (pos+1) to the end
    score = std::stoi(csv.substr(pos + 1));
    std::cout << "Name: " << name << " Score = " << score << std::endl;
  }

This produces the following output.

Name: Evans Score = 87