C++ String at() function
Example
Get the first character (0) of a string:
string txt = "Hello World!";
cout << txt.at(0);
Try it Yourself »
Definition and Usage
The at()
function returns an indexed character from a string.
Tip: To get the last character of a string, you can use the
following code: string.at(string.length()
- 1);
Syntax
string.at(index);
Parameter Values
Parameter | Description |
index | Required. The index (position) of the character to be returned. |
Technical Details
Returns: | A single character, representing the character in the given index position in the string. |
---|
More Examples
Example
Get the last character of a string:
string txt = "Hello World!";
cout << txt.at(txt.length() -
1);
Try it Yourself »
Related Pages
Read more about strings in our String Tutorial.