C++ std::string Äquivalent zu Pythons rstrip
English
Deutsch
In Python kannst du
python_rstrip.py
"test ".rstrip()in C++ ist der einfachste Weg jedoch die Verwendung der Boost String Algorithms-Bibliothek:
cpp_rstrip.cpp
#include <boost/algorithm/string/trim.hpp>
using namespace boost::algorithm;
string to_trim = "test ";
string trimmed = trim_right_copy(to_trim);_copy in trim_right_copy() bedeutet, dass der ursprüngliche String nicht verändert wird.
Vollständiges Beispiel:
cpp_rstrip_full.cpp
#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>
using namespace std;
using namespace boost::algorithm;
int main() {
string to_trim = "test ";
string trimmed = trim_right_copy(to_trim);
cout << trimmed << endl; // Prints 'test'
}Falls du einen benutzerdefinierten Zeichensatz trimmen möchtest, verwende trim_right_copy_if() zusammen mit is_any_of():
cpp_rstrip_custom.cpp
#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>
using namespace std;
using namespace boost::algorithm;
int main() {
string to_trim = "testXbbca";
string trimmed = trim_right_copy_if(to_trim, is_any_of("abc"));
cout << trimmed << endl; // Prints 'testX'
}Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow