String类(下)

简介: String类

String类(上):https://developer.aliyun.com/article/1389435

🌙Element access(元素访问)

这个板块做为了解,知道有就行了😇😇。

💫at函数

返回对字符串中位置pos处的字符的引用。本质上还是一种遍历。

咱们看看代码:

#include <iostream>
#include <string>
int main()
{
    std::string str("Test string");
    for (unsigned i = 0; i < str.length(); ++i)
    {
        std::cout << str.at(i);
    }
    return 0;
}

运行结果:

这里捏还有很多函数就不再细讲咯,有兴趣老铁们翻翻看看。

🌙Modifiers(修改器)

这个接口重重重重点,这里会讲解插入删除.....🤪🤪🤪🤪

💫operator+=插入操作

这种操作本质上是一种赋值运算重载,用起来真的爽,它不仅可以插入新的string对象,还可以插入常量字符串,也可以插入单个字符,我们平时使用最多的方式也是这个方式。

咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s("hello ");
    // 插入常量字符串
    s += "world"; 
    cout << s << endl;
    string str("world");
    // 插入新的string对象
    s += str; 
    cout << s << endl;
    // 插入单个字符
    s += 'A';
    cout << s << endl;
    return 0;
}

运行结果:

💫append插入操作

它可以插入字符串,可以插入另一个string对象,而且可以指定n个字符插入,非常多样化。

 咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s("hello ");
    // 插入常量字符串
    s.append("world");
    cout << s << endl;
    string str("world");
    // 插入另一个string对象
    s.append(str);
    cout << s << endl;
    return 0;
}

运行结果:

💫push_back插入操作

将字符c追加到字符串的末尾,使其长度增加一,也就是说push_back函数只能够尾插入一个字符,不能插入字符串。

 咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s("hello ");
    // 插入一个字符
    s.push_back('C');
    cout << s << endl;
    return 0;
}

运行结果:

💫insert插入操作

insert函数可以在任意的指定位置进行插入。

它可以在任意的指定位置

  • 插入一个新的string对象
  • 一个常量字符串
  • 一个常量字符串的n个字符
  • 一个字符
  • 等等....

 咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s("hello");
    // 在下标为0的位置插入一个新的string对象
    string str("hello world");
    s.insert(0, str);
    cout << s << endl;
    // 在下标为0的位置插入一个常量字符串
    s.insert(0, "hello world");
    cout << s << endl;
    // 在下标为0的位置插入一个常量字符串的前3个字符
    s.insert(0, "hello world", 3);
    cout << s << endl;
    // 在下标为0的位置插入一个字符x
    s.insert(0, 1, 'x');
    s.insert(s.begin(), 'x');
    cout << s << endl;
    // 在下标为0的位置插入三个字符x
    s.insert(0, 3, 'x');
    cout << s << endl;
    return 0;
}

运行结果:

💫erase删除操作

擦除字符串的一部分,缩短其长度,本质上erase函数删除任意指定位置的n个字符

  咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s("hello world");
    // 删除下标为3位置的一个字符
    s.erase(3, 1);
    cout << s << endl;
    // 删除以下标为3位置为起点的3个字符
    s.erase(3, 3);
    cout << s << endl;
    // 删除以下标为3位置为起点往后的所有字符
    s.erase(3);
    cout << s << endl;
    return 0;
}

运行结果:

💫pop_back删除操作

擦除字符串的最后一个字符,有效地将其长度减少一个,本质上可以实现string对象的尾删操作。

  咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s("hello world");
    s.pop_back();
    cout << s << endl;
    return 0;
}

 运行结果:

💫swap交换操作

实现两个对象之间的交换。

  咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1("hello world");
    string s2("string");
    s1.swap(s2);
    cout << s1 << endl;
    cout << s2 << endl;
    return 0;
}

 运行结果:

🌙String operations (字符串操作

       这个板块主要讲解的是,字符串的查找呀,返回值。在许多的题中都运用这块知识,望大家好好体会叭🫣🫣🫣。

💫c_str返回值

返回一个指向数组的指针,该数组包含一个以null结尾的字符序列(即C字符串),咱们可以用这个函数实现返回string对象对应的char * 指针

  咱们看看代码:

//将string对象的内容通过strcpy函数拷贝到字符数组当中
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    string str("hello world");
    char* cstr = new char[str.size() + 1];
    strcpy(cstr, str.c_str());
    cout << cstr << endl;
    return 0;
}

 运行结果:

💫find查找

查找string对象、常量字符串或者是一个字符,并且可以设定pos值来规定查找的起始位置,默认从0下标开始查找。

  咱们看看代码:

// string::find
#include <iostream>       // std::cout
#include <string>         // std::string
int main()
{
    std::string str("There are two needles in this haystack with needles.");
    std::string str2("needle");
    // different member versions of find in the same order as above:
    std::size_t found = str.find(str2);
    if (found != std::string::npos)
        std::cout << "first 'needle' found at: " << found << '\n';
    found = str.find("needles are small", found + 1, 6);
    if (found != std::string::npos)
        std::cout << "second 'needle' found at: " << found << '\n';
    found = str.find("haystack");
    if (found != std::string::npos)
        std::cout << "'haystack' also found at: " << found << '\n';
    found = str.find('.');
    if (found != std::string::npos)
        std::cout << "Period found at: " << found << '\n';
    // let's replace the first needle:
    str.replace(str.find(str2), str2.length(), "preposition");
    std::cout << str << '\n';
    return 0;
}

 运行结果:

💫find查找

rfind是倒着查找。find函数和rfind函数的区别就是查找方向不同。

这里就不再上代码咯。

💫substr

返回string字符串的一个任意子串

咱们看看代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1("hello world");
    // 取出子串"world"
    string s2 = s1.substr(6, 5);
    cout << s2 << endl;
    return 0;
}

运行结果:

到这里还有很多函数没有介绍:

可以去看看官网中它们的使用方法。

🌟结束语

      今天内容就到这里啦,时间过得很快,大家沉下心来好好学习,会有一定的收获的,大家多多坚持,嘻嘻,成功路上注定孤独,因为坚持的人不多。那请大家举起自己的小说手给博主一键三连,有你们的支持是我最大的动力💞💞💞,回见。

目录
相关文章
|
8天前
|
Java 索引
java基础(13)String类
本文介绍了Java中String类的多种操作方法,包括字符串拼接、获取长度、去除空格、替换、截取、分割、比较和查找字符等。
21 0
java基础(13)String类
|
2月前
|
API 索引
String类下常用API
String类下常用API
36 1
|
2月前
for循环和String类下方法的一个练习题
for循环和String类下方法的一个练习题
45 1
|
6天前
|
安全 Java
String类-知识回顾①
这篇文章回顾了Java中String类的相关知识点,包括`==`操作符和`equals()`方法的区别、String类对象的不可变性及其好处、String常量池的概念,以及String对象的加法操作。文章通过代码示例详细解释了这些概念,并探讨了使用String常量池时的一些行为。
String类-知识回顾①
|
19天前
|
存储 安全 Java
Java——String类详解
String 是 Java 中的一个类,用于表示字符串,属于引用数据类型。字符串可以通过多种方式定义,如直接赋值、创建对象、传入 char 或 byte 类型数组。直接赋值会将字符串存储在串池中,复用相同的字符串以节省内存。String 类提供了丰富的方法,如比较(equals() 和 compareTo())、查找(charAt() 和 indexOf())、转换(valueOf() 和 format())、拆分(split())和截取(substring())。此外,还介绍了 StringBuilder 和 StringJoiner 类,前者用于高效拼接字符串,后者用于按指定格式拼接字符串
19 1
Java——String类详解
|
15天前
|
安全 Java
Java StringBuffer 和 StringBuilder 类详解
在 Java 中,`StringBuffer` 和 `StringBuilder` 用于操作可变字符串,支持拼接、插入、删除等功能。两者的主要区别在于线程安全性和性能:`StringBuffer` 线程安全但较慢,适用于多线程环境;`StringBuilder` 非线程安全但更快,适合单线程环境。选择合适的类取决于具体的应用场景和性能需求。通常,在不需要线程安全的情况下,推荐使用 `StringBuilder` 以获得更好的性能。
|
15天前
|
Java 索引
Java String 类详解
Java 中的 `String` 类用于表示不可变的字符序列,是 Java 标准库 `java.lang` 包的一部分。字符串对象一旦创建,其内容不可更改,修改会生成新对象。
|
2月前
|
Java API 索引
【Java基础面试二十四】、String类有哪些方法?
这篇文章列举了Java中String类的常用方法,如`charAt()`、`substring()`、`split()`、`trim()`、`indexOf()`、`lastIndexOf()`、`startsWith()`、`endsWith()`、`toUpperCase()`、`toLowerCase()`、`replaceFirst()`和`replaceAll()`,并建议面试时展示对这些方法的熟悉度,同时深入理解部分方法的源码实现。
【Java基础面试二十四】、String类有哪些方法?
|
9天前
|
Java 索引
java基础扫盲-String类常用的方法
java基础扫盲-String类常用的方法
|
2月前
|
存储 SQL Java
Java 系类之 Java StringBuffer类详解
这篇文章详细介绍了Java中的StringBuffer类的使用,包括其构造方法、追加字符串、替换字符、反转字符串和删除字符串的方法,并提供了相应的示例代码。