迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > 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 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C++ 中通过掷骰子生成随机值

发布时间:2023/04/09 浏览次数:169 分类:C++

本文解释了如何使用时间因子方法和模拟 C++ 中的掷骰子的任意数方法生成随机数。了解它是如何工作的以及它包含哪些缺点。提供了一个 C++ 程序来演示伪数生成器。

在 C++ 中使用模板的链表

发布时间:2023/04/09 浏览次数:158 分类:C++

本文解释了使用模板在 C++ 中创建链表所涉及的各个步骤。工作程序演示了一个链表,该链表使用模板来避免在创建新变量时声明数据类型的需要。

在 C++ 中添加定时延迟

发布时间:2023/04/09 浏览次数:142 分类:C++

本教程将为你提供有关在 C++ 程序中添加定时延迟的简要指南。这可以使用 C++ 库为我们提供的一些函数以多种方式完成。

在 C++ 中创建查找表

发布时间:2023/04/09 浏览次数:155 分类:C++

本文重点介绍如何创建查找表及其在不同场景中的用途。提供了三个代码示例以使理解更容易,并附有代码片段以详细了解代码。

如何在 C++ 中把字符串转换为小写

发布时间:2023/04/09 浏览次数:63 分类:C++

介绍了如何将 C++ std::string 转换为小写的方法。当我们在考虑 C++ 中的字符串转换方法时,首先要问自己的是我的输入字符串有什么样的编码

如何在 C++ 中确定一个字符串是否是数字

发布时间:2023/04/09 浏览次数:163 分类:C++

本文介绍了如何检查给定的 C++ 字符串是否是数字。在我们深入研究之前,需要注意的是,以下方法只与单字节字符串和十进制整数兼容。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便