Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Strings basics
7. Substrings
Regular expressions
Modifying strings
Summary

Instruction

Okay, time to move to some more complex string operations. Let's start with the substr() function, which allows us to select part of each string (i.e., a substring) in a vector. The arguments are the vector of strings, where we start the selection in each string, and where we stop.

We have in memory two vectors containing the names of cities and countries where our clients live. If we wanted to get only the first two letters of each city name, we can use the substr() function in the following way:

substr(cities, start = 1, stop = 2)

The start and stop parameters are indexes of the first and last characters of the substring we want to get.

Exercise

From the countries vector, get the first three letters of each country's name. Use the substr() function.