C++/C--刪除string末尾字符的方法【轉載】

1 刪除字符串最後一個字符的方法

代碼實現:

#include<iostream>
#include<string>
using namespace std;
int main() 
{
	string str;
	str = "123456";
	cout << str << endl;
 
	//方法一:使用substr()
	str = str.substr(0, str.length() - 1);
	cout << str << endl;
 
	//方法二:使用erase()
	str.erase(str.end() - 1);
	cout << str << endl;
 
	//方法三:使用pop_back()
	str.pop_back();
	cout << str << endl;
	return 0;
}

運行結果:
在這裏插入圖片描述


以上內容來自:

  1. HarryLi_C++刪除string最後一個字符的幾種方法【CSDN】
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章