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

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Access Individual Character Elements Of A C++ String
By Yilmaz Yoru July 30, 2021

How can I access a character of a string? How can I use the at() method of strings? Can I use front() and back() methods in std::string to access characters?

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,
C++:
std::string str = "This is a String";
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.

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,
C++:
#include <iostream>
#include <string>
 
int main()
{
 std::string str = "ABCD";
 
 char c;
 c = str[0];     // set a char to first char of string
 std::cout << (char)(c) << std::endl;  // printing a character
 
 std::cout << str[1] << std::endl;  // printing character of a string with [ ]
 
 getchar();
 return 0;
}
and the string outputs will be,
Код:
A
B
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,
C++:
#include <iostream>
#include <string>
 
int main()
{
 std::string str = "ABCD";
 
 std::cout << str.at(0) << std::endl; // printing character of a string
 
 std::cout << str << std::endl;  // printing a string
 
 str.at(0) = (char)(88); // set first char to 'X'
 std::cout << str << std::endl;
 
 str.at(1) = '-';        // set second char to '-'
 std::cout << str << std::endl;
 
 str.at(3) = str.at(2);  // set forth char to third one, copy one char to another index
 std::cout << str << std::endl;
 
 getchar();
 return 0;
}
and the string outputs will be,
Код:
A
ABCD
XBCD
X-CD
X-CC
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,
C++:
#include <iostream>
#include <string>
 
int main()
{
 std::string str = "ABCD";
 
 std::cout << str.front() << std::endl;
 
 std::cout << str.back() << std::endl;
 
 getchar();
 return 0;
}
and the string outputs will be,
Код:
A
D
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.