এখানে আমরা দেখব কিভাবে C++ এ স্ট্রিং ট্রিম করা যায়। ট্রিমিং স্ট্রিং মানে স্ট্রিং এর বাম এবং ডান অংশ থেকে সাদা স্পেস অপসারণ করা।
C++ স্ট্রিং ট্রিম করতে, আমরা বুস্ট স্ট্রিং লাইব্রেরি ব্যবহার করব। সেই লাইব্রেরিতে, trim_left() এবং trim_right() নামে দুটি ভিন্ন পদ্ধতি রয়েছে। স্ট্রিং সম্পূর্ণভাবে ছাঁটাই করতে, আমরা উভয়ই ব্যবহার করতে পারি।
উদাহরণ
#include<iostream>
#include<boost/algorithm/string.hpp>
using namespace std;
main(){
string myStr = " This is a string ";
cout << "The string is: (" << myStr << ")" << endl;
//trim the string
boost::trim_right(myStr);
cout << "The string is: (" << myStr << ")" << endl;
boost::trim_left(myStr);
cout << "The string is: (" << myStr << ")" << endl;
} আউটপুট
$ g++ test.cpp $ ./a.out The string is: ( This is a string ) The string is: ( This is a string) The string is: (This is a string) $