সম্পাদনা সি ++ 17 সাল থেকে স্ট্যান্ডার্ড লাইব্রেরির কিছু অংশ সরানো হয়েছে। ভাগ্যক্রমে, সি ++ 11 দিয়ে শুরু করে, আমাদের ল্যাম্বডাস রয়েছে যা একটি উচ্চতর সমাধান।
#include <algorithm>
#include <cctype>
#include <locale>
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}
আধুনিক সমাধান আনার জন্য https://stackoverflow.com/a/44973498/524503 ধন্যবাদ
আসল উত্তর:
আমি আমার ছাঁটাই প্রয়োজনীয়তার জন্য এই 3 টির মধ্যে একটির ব্যবহার করতে চাই:
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
এগুলি মোটামুটি স্ব-বর্ণনামূলক এবং খুব ভালভাবে কাজ করে।
সম্পাদনা : বিটিডাব্লু, আমি std::ptr_fun
সেখানে অসম্পূর্ণ সাহায্য করতে পারি std::isspace
কারণ সেখানে একটি দ্বিতীয় সংজ্ঞা রয়েছে যা লোকালগুলিকে সমর্থন করে। এটি ঠিক একইরকম castালাই হতে পারে তবে আমি এটি আরও ভাল পছন্দ করি।
সম্পাদনা : পরামিতিটি রেফারেন্স, সংশোধন করে এবং ফিরিয়ে দেওয়ার বিষয়ে কিছু মন্তব্যকে সম্বোধন করার জন্য। আমি রাজী. এমন একটি বাস্তবায়ন যা আমি সম্ভবত পছন্দ করবো তা হ'ল দুটি সেট ফাংশন, একটি স্থানের জন্য এবং একটি যা অনুলিপি করে। উদাহরণগুলির আরও ভাল সেট হ'ল:
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}
প্রসঙ্গের জন্য এবং উচ্চ ভোটের উত্তরটি এখনও উপলব্ধ রাখার স্বার্থে আমি মূল উত্তরটি উপরে রাখছি।