C++C++11C++14C++17Introduction to C++Learn C++

How To Access Individual Character Elements Of A C++ String

Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std:string types that are defined in header <string>.

strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other terms, string stores the alphanumeric text with 1-byte chars called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,

The string has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can access characters with [ ] brackets; we can use at()front(), and back() methods to add chars to a string. 

In this post, you’ll learn how to access a character of an astring, how to use the at() method of strings, and how to use the front() and back() methods in std::string to access characters. By learning these character elements of a c++ string, it will help you build C++ applications with the use of C++ software.

How Do I access a single character of a String in C++?

Strings are a class contains arrays of characters with useful methods, and we can access, or modify their characters easily. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘. We can access characters with [ ] brackets; we can use at(), front() and back() methods to add chars to a string. Let’s see how we access and modify characters of strings.

Accessing a Character of a String with [ ] brackets

This example below shows how we can access to characters of a string by using [ ] brackets,

and the string outputs will be,

as you see in this example we print the first and second characters of the string.

You can access to a Character of a String with the at() method

We can access any character of a string by using at() method. This example below shows how we can access to characters of a string by using at() method,

and the string outputs will be,

as you see in this example we print the first and second characters of the string.

How to grab a portion of a C++ String with the front() and back() methods

We can easily reach to the first and the last char of a string by using front() and back() methods. This example below shows how we can access to characters of a string by using front() and back() methods,

and the string outputs will be,

As you see we can easily reach to the first and last char of a string. Sometimes it is important to check first or last char, if it is a number, if it is a question mark, etc.

Note that, these methods above can be used with std::wstring, std::u16string, std::u32string too.


C++ Builder is superbly powerful yet easy to use IDE for developing C++ programs. Why not download a free trial today?

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap