string at() in C++ Last Updated : 04 Oct, 2024 Comments Improve Suggest changes Like Article Like Report The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturns the character at the given index of the string.If the given index is less than 0 or greater than equal to the length of string, then it throws an out_of_range exception.Example of string::at() C++ // C++ Program to show, how to use string::at() // for extracting the character from given index #include <bits/stdc++.h> using namespace std; int main() { string str("GeeksForGeeks"); // Extracting the character of 0th index cout << str.at(0) << endl; // Extracting the character of 5th index cout << str.at(5) << endl; // Extracting the character of 9th index cout << str.at(9) << endl; return 0; } OutputG F e Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article string at() in C++ P Pushpanjali Chauhan Improve Article Tags : Misc C++ STL CPP-Functions cpp-strings-library +1 More Practice Tags : CPPMiscSTL Similar Reads Strings in C++ In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s 5 min read Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i 5 min read std::basic_string::at in C++ Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po 1 min read Array of Strings in C++ In C++, a string is sequence of characters that is used to store textual information. Internally, it is implemented as a dynamic array of characters. Array of strings is the array in which each element is a string.We can easily create an array of string in C++ as shown in the below example:C++#inclu 4 min read String find() in C++ In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Welcome to GfG!"; s 4 min read std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign 5 min read std::string::append() in C++ The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C 3 min read string erase in C++ The string erase() function is a built-in function of the string class that is used to erase the whole or part of the string, shortening its length. The function provides different ways to delete a portion of a string based on either an index position or a range of characters or delete the whole str 4 min read std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po 2 min read String Functions in C++ A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar 8 min read Like