C++ String Basics
January 25, 2019
In this chapter we are going to explore the basics of strings as well as introduce functions and errors.
Strings
A string is another datatype in C++. A string is a sequence of characters. Examples of strings are "Hello World!"
, "I am learning C++"
, etc. Even "423"
is a string, if we write inside double quotation marks.
It is part of the Standard Library in C++. Standard Library is a collection of some libraries which contains functions and data types to perform some tasks. For string, we use the <string>
library.
#include <iostream>
#include <string> // library for strings
using namespace std;
int main() {
string a = "I am learning C++" ;
cout << a << endl;
return 0;
}
The
<string>
library is included in the<iostream>
library, so you don’t need to include<string>
separately if you already use<iostream>
. However, it is recommended practice to include the library directly if you are using it.
Strings can be strung together. Try this:
string name = "Commonlounge";
cout << "Hi there " + name << endl;
Notice how we had to end the first string ("Hi there "
) with a space (" "
). If we did not, then our result would be "Hi thereCommonlounge"
, which isn’t what we want.
If you need to put double quotes inside your string, you have to escape the apostrophe with a backslash (\
):
cout << "He said, \"Strings are so cool!\"";
// Output - He said, "Strings are so cool!"
Functions
If you want to calculate the length of string, then simply use length()
function. For example:
string name = "Commonlounge";
cout << name.length() << endl;
// Output 12
You just used the length
function on your string! A function (like length()
) is a sequence of instructions that C++ has to perform on a given object ("Commonlounge"
) once you call it.
Note: We’ll be learning more about C++ Strings and Functions later in the course.
Errors
Let’s try something new. Can we get the length of a number the same way we could find out the length of our name? Type in
int a = 6;
cout << a.length() << endl;
// Outputs - error: request for member 'length' in 'a', which is of non-class type 'int'
We got our first error! Making mistakes (even intentional ones) are an important part of learning! In particular, an error doesn’t mean something bad is going to happen with the computer. It’s just C++‘s way of saying, “Something’s wrong.” or “I don’t understand.”
It says that objects of type int
(integers, whole numbers) have no length
. So what can we do now? Maybe we can write our number as a string? Strings have a length, right?
Converting between Strings and other data-types
int a = 304023;
cout << to_string(a).length() << endl;
// Outputs 6
It worked! We used the to_string()
function to convert integer to string and then used the .length()
function to calculate length of the given string. The step-by-step evaluation of the above expression is:
to_string(a).length()
=> to_string(304023).length()
=> "304023".length()
=> 6
- You can use the
to_string()
function to convert anything to astring
. - You can use the
stoi()
function to convert astring
to anint
. - You can use the
stof()
function to convert astring
to afloat
.
Important: we can convert numbers into text, but we can’t necessarily convert text into numbers – what would
stoi('hello')
be anyway?
Wonder why sometimes you call functions with a .
at the end of the value (like name.length()
) and sometimes you first call a function and place the value in parentheses (like to_string(304023)
)?
Well, in some cases, functions belong to objects, like .length()
, which can only be performed on strings.
Other times, functions don’t belong to anything specific and can be used on different types of objects, just like to_string()
. That’s why we’re giving 304023
as a parameter to the to_string()
function.
Summary
OK, enough of strings, functions and errors. So far you’ve learned about:
- strings – in C++, strings are used for text objects.
- functions – like
.length()
andto_string()
, perform actions on objects. - errors – you now know how to read and understand errors that show up if C++ doesn’t understand a command you’ve given it.
There was quite a lot you covered! The next quiz will make sure you understood the topics well. Ready?