迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

从 C++ 中的字符串中获取最后一个字符

作者:迹忆客 最近更新:2023/03/31 浏览次数:

本文将教授 C++ 中从字符串中获取最后一个字符的不同方法。


在 C++ 中使用 std::string::at 从字符串中获取最后一个字符

我们可以使用 std::string::at 从字符串中提取出现在给定位置的字符。

语法:

char& string::at(size_type indexNo)

这将返回用户指定的 indexNo 处指定位置的字符。当传递了无效的索引号时,该方法会抛出 out_of_range 异常,例如小于零的索引或大于或等于 string.size() 的索引。

示例:提取最后一个字符和给定位置的字符。

#include <iostream>
using namespace std;

int main()
{
    string str("Spider Man");

    cout<<"Character at position 3: "<<str.at(3)<<endl;

    int length = str.size();

    cout<<"Character at last position: "<<str.at(length-1)<<endl;

}

输出:

Character at position 3: d
Character at last position: n

在 C++ 中使用 std::string::back() 从字符串中获取最后一个字符

C++ 中的 std::string::back() 给出了对字符串最后一个字符的引用。这仅适用于非空字符串。

此函数还可以替换字符串末尾的字符。

语法:

  1. 访问最后一个字符。
char ch = string_name.back();
  1. 替换出现在字符串末尾的字符。
str.back() = 'new_char_we_to_replace_with';

此函数不带参数,它提供对字符串最后一个字符的引用。当应用于空字符串时,它会显示未定义的行为。

示例代码:

#include <iostream>
using namespace std;

int main()
{
    string str("Spider Man");
    char ch = str.back();

    cout<<"Character at the end "<<ch<<endl;

    str.back() = '@';

    cout<<"String after replacing the last character: "<<str<<endl;

}

输出:

Character at the end n
String after replacing the last character: Spider Ma@

使用 std::string::rbegin() 从 C++ 中的字符串中获取最后一个字符

C++ 中的 std::string::rbegin() 代表反向开始。该函数用于指向字符串的最后一个字符。

语法:

reverse_iterator it = string_name.rbegin();

该函数不包含任何参数,它返回指向字符串最后一个字符的反向迭代器。

示例代码:

#include <iostream>
#include<string>
using namespace std;

int main()
{
    string str("Spider Man");

    string::reverse_iterator it = str.rbegin();

    cout<<"The last character of the string is: "<<*it;

}

输出:

The last character of the string is: n

在 C++ 中使用 std::string:begin()reverse() 从字符串中获取最后一个字符

C++ 中的 std::string::begin() 将迭代器返回到字符串的开头,不带参数。

语法:

string::iterator it = string_name.begin();

std::string::reverse() 是 C++ 中用于直接反转字符串的内置函数。它将 beginend 迭代器作为参数。

该函数在 algorithm 头文件中定义。

语法:

reverse(string_name.begin(), string_name.end());

要从字符串中获取最后一个字符,我们首先使用 reverse 方法反转字符串,然后使用 begin() 方法获取字符串第一个字符的引用(这是反转之前的最后一个字符)并打印它。

示例代码:

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    string str("Spider Man");

    reverse(str.begin(),str.end());

    string::iterator it = str.begin();

    cout<<"The last character of the string is: "<<*it;

}

输出:

The last character of the string is: n

在 C++ 中使用数组索引访问字符串的字符

我们可以通过使用它的索引号引用它并在方括号 [] 中传递它来访问字符串的字符。

示例代码:

#include <iostream>

using namespace std;

int main()
{
    string str("Bruce banner");

    int length = str.size();

    cout<<"The last character of the string is "<<str[length-1];

}

为了得到最后一个字符,我们首先找到字符串的长度并在方括号中传递 length-1

输出:

The last character of the string is r

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便