前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++奇迹之旅:string类对象的修改操作

C++奇迹之旅:string类对象的修改操作

作者头像
学习起来吧
发布2024-05-07 08:33:27
1010
发布2024-05-07 08:33:27
举报
文章被收录于专栏:学习C/++学习C/++

📝string类的常用接口

string网址查询:https://legacy.cplusplus.com/reference/string/string/

🌠 string类对象的修改操作

函数名称

功能说明

push_back

在字符串后尾插字符c

append

在字符串后追加一个字符串

operator+= (重点)

在字符串后追加字符串str

c_str(重点)

返回C格式字符串

find + npos(重点)

从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置

rfind

从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置

substr

在str中从pos位置开始,截取n个字符,然后将其返回

🌉push_back

将字符 c 追加到字符串的末尾,将其长度增加 1

代码语言:javascript
复制
string s1("hello world");
cout << s1 << endl;
cout << s1.size() << endl;

s1.push_back('x');
cout << s1 << endl;
cout << s1.size() << endl;

🌉append

  1. 在字符串后追加字符串
代码语言:javascript
复制
string s2("hello world");
s2.append(" yyyyyy");
cout << s2 << endl;
cout << s2.size() << endl;
cout << endl;
代码语言:javascript
复制
string s1("hello String");
s1.push_back('!');
cout << s1 << endl;

string s2("hello world");
s2.append(" yyyyyy");
cout << s2 << endl;
cout << s2.size() << endl;
cout << endl;

s1.append(s2);
cout << s2 << endl;
  1. 可以使用迭代器取某一部分字符串
代码语言:javascript
复制
string s1("hello String");
string s2("hello world");
s1.append(s2.begin() + 6, s2.end());
cout << s1 << endl;

综合其用法:

代码语言:javascript
复制
string str;
string str2 = "Writing ";
string str3 = "print 10 and then 5 more";

// used in the same order as described above:
str.append(str2);                       // "Writing "
str.append(str3, 6, 3);                   // "10 "
str.append("dots are cool", 5);          // "dots "
str.append("here: ");                   // "here: "
str.append(10u, '.');                    // ".........."
str.append(str3.begin() + 8, str3.end());  // " and then 5 more"

std::cout << str << '\n';
return 0;

🌉operator+=

用法:通过在当前值的末尾附加其他字符来扩展字符串:可以追加这string对象,字符串,字符 例子:

代码语言:javascript
复制
string name("John");
string family("Smith");
name += "K ";
name += family;
name += '\n';

cout << name << endl;

🌉insert

例如:在字符串中指定位置插入其他字符

代码语言:javascript
复制
string s1("to be question");
string s2("the ");
string s3("or not to be");

s1.insert(6, s2);
cout << s1 << endl;

s1.insert(6, s3, 3, 4);
cout << s1 << endl;

s1.insert(10, "that is cool", 8);
cout << s1 << endl;

🌉erase

功能:擦除部分字符串,减少其长度: 三种擦除:顺序擦除,指定擦除,范围擦除

代码语言:javascript
复制
string s1("This is an example sentence.");
cout << s1 << endl;

//顺序擦除
s1.erase(10, 8);
cout << s1 << endl;

//指定擦除
s1.erase(s1.begin() + 9);
cout << s1 << endl;

//范围擦除:擦除 [first,last] 范围内的字符序列。
s1.erase(s1.begin() + 5, s1.end() - 9);
cout << s1 << endl;

🌉replace

功能:用新内容替换字符串中从字符 pos 开始并跨越 len 字符的部分(或字符串在 [i1,i2) 之间的部分):

代码语言:javascript
复制
string base("this is a test string.");
string s2("n example");
string s3("sample phrase");

string s1 = base;
s1.replace(9, 5, s2);
cout << s1 << endl;

s1.replace(19, 6, s3, 7, 6);
cout << s1 << endl;

详细可查看:https://legacy.cplusplus.com/reference/string/string/replace/

🌉 find

作用:用于在字符串中搜索指定子字符串或字符的第一次出现。 std::string::nposstd::string类的一个静态成员常量,表示当搜索的子字符串或字符未找到时,npos为无效值。

  1. 在字符串中搜索子字符串的位置:
代码语言:javascript
复制
#include <iostream>
#include <string>

int main() 
{
    std::string str = "The quick brown fox jumps over the lazy dog.";
    std::string substr = "brown";

    size_t pos = str.find(substr);
    if (pos != std::string::npos) 
    {
        std::cout << "Found '" << substr << "' at position: " << pos << std::endl;
    } else 
    {
        std::cout << "'" << substr << "' not found in the string." << std::endl;
    }

    return 0;
}

输出:

代码语言:javascript
复制
Found 'brown' at position: 10
  1. 从指定位置开始搜索子字符串:
代码语言:javascript
复制
#include <iostream>
#include <string>

int main() 
{
    std::string str = "The quick brown fox jumps over the lazy dog.";
    std::string substr = "the";

    size_t pos = str.find(substr, 15);
    if (pos != std::string::npos) 
    {
        std::cout << "Found '" << substr << "' at position: " << pos << std::endl;
    } else 
    {
        std::cout << "'" << substr << "' not found in the string." << std::endl;
    }

    return 0;
}

输出:

代码语言:javascript
复制
Found 'the' at position: 36
  1. 搜索字符:
代码语言:javascript
复制
#include <iostream>
#include <string>

int main() 
{
    std::string str = "Hello, World!";
    char c = 'o';

    size_t pos = str.find(c);
    if (pos != std::string::npos) 
    {
        std::cout << "Found '" << c << "' at position: " << pos << std::endl;
    } else {
        std::cout << "'" << c << "' not found in the string." << std::endl;
    }

    return 0;
}

输出:

代码语言:javascript
复制
Found 'o' at position: 4

🌉 c_str

它返回一个指向字符串内容的 C 风格字符串指针(以 null 字符结尾)。这个函数非常有用,因为它允许你将 std::string 对象传递给需要 C 风格字符串的函数

代码语言:javascript
复制
string str = "Hello,World!";

//使用 c_str() 获取 C 风格字符串
const char* cstr = str.c_str();
cout << cstr << endl;

🌠测试string

代码语言:javascript
复制
// 测试string:
// 1. 插入(拼接)方式:push_back  append  operator+= 
// 2. 正向和反向查找:find() + rfind()
// 3. 截取子串:substr()
// 4. 删除:erase
void Teststring()
{
	string str;
	str.push_back(' '); // 在str后插入空格
	str.append("Hello");// 在str后追加一个字符"hello"
	str += 'S'; // 在str后追加一个字符'S' 
	str += "tring"; //在str后追加一个字符串"tring"
	cout << str << endl;
	cout << str.c_str() << endl;

	//获取file的后缀
	string file("string.cpp");
	size_t pos = file.rfind('.');
	string suffix(file.substr(pos, file.size() - pos));
	cout << suffix << endl;

	//npos是string里面的一个静态变量
	//static const size_t npos = -1;

	//取出url中的域名
	string url("https://legacy.cplusplus.com/reference/string/string/find/");
	cout << url << endl;
	size_t start = url.find("://");
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	start += 3;
	size_t finsh = url.find('/', start);
	string address = url.substr(start, finsh - start);
	cout << address << endl;

	//删除url的协议前缀
	pos = url.find("://");
	url.erase(0, pos + 3);
	cout << url << endl;
}

int main()
{
	Teststring();
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 📝string类的常用接口
  • 🌠 string类对象的修改操作
    • 🌉push_back
      • 🌉append
        • 🌉operator+=
          • 🌉insert
            • 🌉erase
              • 🌉replace
                • 🌉 find
                  • 🌉 c_str
                  • 🌠测试string
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档