C++ String at() 函数
定义和用法
at()
函数返回字符串中指定索引位置的字符。
提示:要获取字符串的最后一个字符,可以使用以下代码:
string.at(string.length() - 1);
实例
例子 1
获取字符串的第一个字符(索引为 0):
string txt = "Hello World!"; cout << txt.at(0);
例子 2
获取字符串的最后一个字符:
string txt = "Hello World!"; cout << txt.at(txt.length() - 1);
语法
string.at(index);
参数
参数 | 描述 |
---|---|
index | 必需。要返回的字符的索引(位置)。 |
技术细节
返回: | 单个字符,表示字符串中给定索引位置的字符。 |
---|
相关页面
教程:C++ 字符串