此函數包含在“boost/algorithm/string” 庫中。 Boost 字符串算法庫提供了 STL 中缺少的 string-related 算法的通用實現。 trim 函數用於從字符串中刪除所有前導或尾隨空格。輸入序列已就地修改。
- trim_left():從字符串中刪除所有前導空格。
- trim_right():從字符串中刪除所有尾隨空格。
- trim():從字符串中刪除所有前導和尾隨空格。
用法:
Template:trim(Input, Loc);
參數:
Input:輸入序列
Loc:用於‘space’ 分類的語言環境
返回:修改後的輸入序列沒有前導或尾隨空格。
例子:
Input:” geeks_for_geeks ”
Output:Applied left trim:“geeks_for_geeks ”
Applied right trim:” geeks_for_geeks”
Applied trim:“geeks_for_geeks”
Explanation:
The trim_left() function removes all the leading white spaces.
The trim_right() function removes all the trailing white spaces.
The trim() function removes all the leading and trailing white spaces.
下麵是使用函數boost::trim() 從字符串中刪除空格的實現:
C++
// C++ program to remove white spaces
// from string using the function
// boost::trim function
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace boost::algorithm;
using namespace std;
// Driver Code
int main()
{
// Given Input
string s1 = " geeks_for_geeks ";
string s2 = " geeks_for_geeks ";
string s3 = " geeks_for_geeks ";
// Apply Left Trim on string, s1
cout << "The original string is:\""
<< s1 << "\" \n";
trim_left(s1);
cout << "Applied left trim:\""
<< s1 << "\" \n\n";
// Apply Right Trim on string, s2
cout << "The original string is:\""
<< s2 << "\" \n";
trim_right(s2);
cout << "Applied right trim:\""
<< s2 << "\" \n\n";
// Apply Trim on string, s3
cout << "The original string is:\""
<< s3 << "\" \n";
trim(s3);
cout << "Applied trim:\"" << s3
<< "\" \n";
return 0;
}
The original string is:" geeks_for_geeks " Applied left trim:"geeks_for_geeks " The original string is:" geeks_for_geeks " Applied right trim:" geeks_for_geeks" The original string is:" geeks_for_geeks " Applied trim:"geeks_for_geeks"
時間複雜度:O(N)
輔助空間:O(1)
相關用法
- C++ boost::algorithm::none_of_equal()用法及代碼示例
- C++ boost::algorithm::one_of_equal()用法及代碼示例
- C++ boost::algorithm::all_of()用法及代碼示例
- C++ boost::algorithm::all_of_equal()用法及代碼示例
- C++ boost::algorithm::one_of()用法及代碼示例
- C++ boost::algorithm::is_sorted()用法及代碼示例
- C++ boost::algorithm::any_of_equal()用法及代碼示例
- C++ boost::algorithm::any_of()用法及代碼示例
- C++ boost::algorithm::none_of()用法及代碼示例
- C++ boost::algorithm::is_partitioned()用法及代碼示例
注:本文由純淨天空篩選整理自newcybergypsy大神的英文原創作品 boost::trim in C++ library。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。