string(2)

string类的常见接口

string类对象的修改操作

▲表示重要函数

函数功能
operator+=▲在字符串后追加字符串str
append在字符串后追加一个字符串
push_back在字符串后尾插字符c
assign将内容分配给字符串
insert在字符串中插入字符串或字符
erase在字符串中删除字符或字符串
replace替换字符串的一部分
swap交换字符串
pop_back字符串的尾删
c_str▲返回字符串的指针
copy拷贝字符串的一部分
find+npos▲从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
find_first_of在字符串中从前往后搜索与其参数中指定的任何字符匹配的第一个字符
find_last_of在字符串中从后往前搜索与其参数中指定的任何字符匹配的第一个字符
find_first_not_of在字符串中从前往后搜索与其参数中指定的任何字符不匹配的第一个字符
find_last_not_of在字符串中从后往前搜索与其参数中指定的任何字符不匹配的第一个字符
substr在str中从pos位置开始,截取n个字符,然后将其返回
  • operator+=

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test()
{
	string s("hello world");
	cout << s << endl;

	s += "x";
	cout << s << endl;

	s += "yyyyy";
	cout << s << endl;

	string ss("111");
	s += ss;
	cout << s << endl;
}


int main()
{
	Test();
	return 0;
}

在这里插入图片描述

  • append

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test1()
{
	string s("hello world");
	cout << s << endl;

	s.append("x");
	cout << s << endl;

	s.append("yyy", 2);
	cout << s << endl;

}

int main()
{
	Test1();
	return 0;
}

在这里插入图片描述

  • push_back

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test2()
{
	string s("hello world");
	cout << s << endl;

	s.push_back('x');
	cout << s << endl;
}

int main()
{
	Test2();
	return 0;
}

在这里插入图片描述

  • assign

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test3()
{
	string s("hello world");
	cout << s << endl;

	s.assign("x");
	cout << s << endl;

	s.assign("yyy", 2);
	cout << s << endl;
}

int main()
{
	Test3();
	return 0;
}

在这里插入图片描述

  • insert

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test4()
{
	string s("hello world");
	cout << s << endl;
	//头插
	s.insert(0,"x");
	cout << s << endl;

	s.insert(5,"yyy", 2,1);
	cout << s << endl;

	//尾插
	s.insert(s.size(), "y");
	cout << s << endl;
}

int main()
{
	Test4();
	return 0;
}

在这里插入图片描述

  • erase

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test5()
{
	string s("hello world");
	cout << s << endl;
	//头删
	s.erase(0, 1);
	cout << s << endl;

	//尾删
	s.erase(s.size() - 1, 1);
	cout << s << endl;

	s.erase(5);
	cout << s << endl;
}

int main()
{
	Test5();
	return 0;
}

在这里插入图片描述

  • replace

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test6()
{
	string s("hello world");
	cout << s << endl;
	
	s.replace(5, 1, "%");
	cout << s << endl;

	s.replace(5, 2, "yyyyyy", 2, 2);
	cout << s << endl;

	s.replace(5, 2, "ffffffff", 5);
	cout << s << endl;
}

int main()
{
	Test6();
	return 0;
}

在这里插入图片描述

  • swap

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test7()
{
	string s("hello world");
	cout << s << endl;

	string ss("aaaaaaaaa");
	cout << ss << endl << endl;

	s.swap(ss);
	cout << s << endl;
	cout << ss << endl;
}

int main()
{
	Test7();
	return 0;
}

在这里插入图片描述

  • pop_back

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test8()
{
	string s("hello world");
	cout << s << endl;

	s.pop_back();
	cout << s << endl;
}

int main()
{
	Test8();
	return 0;
}

在这里插入图片描述

  • c_sttr

在这里插入图片描述

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

void Test9()
{
	string str("hello world");
	char* cstr = new char[str.size() + 1];
	strcpy(cstr, str.c_str());
	cout << cstr << endl;
}

int main()
{
	Test9();
	return 0;
}

在这里插入图片描述

  • copy

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void Test10()
{
	string s("hello world");
	cout << s << endl;
	char str[20];

	size_t len=s.copy(str, 5, 6);
	str[len] = '\0';
	cout << str << endl;
}

int main()
{
	Test10();
	return 0;
}

在这里插入图片描述

  • find+npos
    在这里插入图片描述
    在这里插入图片描述
#include<iostream>
#include<string>

using namespace std;

void Test11()
{
	string s("hello    world");
	cout << s << endl;

	size_t pos=s.find(" ");
	while (pos != string::npos)
	{
		s.replace(pos, 1, "$$");
		cout << s << endl;
		pos = s.find(" ", pos + 2);
	}
}

int main()
{
	Test11();
	return 0;
}

在这里插入图片描述

  • rfind

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;
void Test12()
{
	string s("hello     world");
	cout << s << endl;

	size_t pos = s.rfind(" ");
	while (pos != string::npos)
	{
		s.replace(pos, 1, "$");
		cout << s << endl;

		pos = s.rfind(" ", pos-1);
	}
}

int main()
{
	Test12();
	return 0;
}

在这里插入图片描述

  • find_first_of

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;
void Test13()
{
	string s("hello world");
	cout << s << endl;

	size_t pos = s.find_first_of("lo");
	while (pos != string::npos)
	{
		s[pos] = '*';
		cout << s << endl;
		pos = s.find_first_of("lo", pos + 1);
	}
	cout << s << endl;
}

int main()
{
	Test13();
	return 0;
}

在这里插入图片描述

  • find_last_of

在这里插入图片描述
分别取文件的路径和文件名:

#include<iostream>
#include<string>

using namespace std;
void Test14()
{
	string s1("/usr/bin/man");
	string s2("c:\\windows\\winhelp.exe");
	cout << s1 << endl;

	size_t pos = s1.find_last_of("/\\");
	cout << "path:" << s1.substr(0, pos) << endl;
	cout << "file:" << s1.substr(pos + 1) << endl;

	cout << s2 << endl;
	pos = s2.find_last_of("/\\");
	cout << "path:" << s2.substr(0, pos) << endl;
	cout << "file:" << s2.substr(pos + 1) << endl;
}

int main()
{
	Test14();
	return 0;
}

在这里插入图片描述

  • find_first_not_of

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;
void Test15()
{
	string s("hello world");
	cout << s << endl;

	size_t pos = s.find_first_not_of("lo");
	while (pos != string::npos)
	{
		s[pos] = '*';
		cout << s << endl;

		pos = s.find_first_not_of("lo",pos+1);
	}
}

int main()
{
	Test15();
	return 0;
}

在这里插入图片描述

  • find_last_not_of

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;
void Test16()
{
	string s("Please, erase trailing white-spaces   \n");
	cout << s << endl;

	size_t pos = s.find_last_not_of(" \t\f\v\n\r");
	if (pos != string::npos)
		s.erase(pos+1);
	else
		s.clear();
	std::cout << '[' << s << "]\n";
}

int main()
{
	Test16();
	return 0;
}

在这里插入图片描述

  • substr

在这里插入图片描述

void Test17()
{
	string s("hello world");
	cout << s << endl;

	cout << s.substr(6, 5) << endl;
}

int main()
{
	Test17();
	return 0;
}

在这里插入图片描述

字符串最后一个单词的长度

字符串最后一个单词的长度
在这里插入图片描述

思路:不能使用cin作为输入,因为cin在遇到空格时就会结束,可以使用getline代替

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

int main() {
    string line;
    while(getline(cin, line))
    {
        size_t pos = line.rfind(' ');
        cout<<line.size()-pos-1<<endl;
    }
    return 0;
}

在这里插入图片描述

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值