Leeren String Inhalt löschen?
-
Dann eben so
pos1 = str.find_first_not_of(" \t"); pos2 = str.find_last_not_of(" \t");oder
#include <boost/algorithm/string/trim.hpp> string tmp(" hallo das ist ein test "); boost::trim(tmp);
-
Und was ist mit \n, \r, \0 und \x0B ? :p
-
Das sind aber dann keine Leerzeichen.

-
Wenn schon denn schon. Alles radikal weg.

Komm nur drauf weil zB bei PHP es die trim-Funktion so handhabt.
-
ansonsten fällt mir boost::regex ein
edit: oder boost::trim_if
#include <boost/algorithm/string.hpp> #include <string> #include <iostream> using namespace std; bool is_whitespace(char c) { return c == '\r' || c == '\n' || c == '\t' || c == ' '; } int main(int argc, char *argv[]) { string s = " \t\r\nText \r\n\t "; boost::trim_if(s, is_whitespace); cout << s << endl; cin.get(); return EXIT_SUCCESS; }
-
kernel64 schrieb:
Habs so gemacht, ist das ok so?
string Trim(string str) { int pos1, pos2; string tmp; pos1 = str.find_first_not_of(" "); pos2 = str.find_last_not_of(" "); tmp = str.substr(pos1, pos2+1); return tmp; }Du kannst dir auch tmp sparen und direkt mit str arbeiten.
Du hast folgende Möglichkeiten dir dein trim() zu erstellen:string str = " dhfdg "; str = trim(str); // oder trim(str);Ich würde ersteres bevorzugen, also ein string trim( string str ) ( kein const string& str, da es ja ehe in der Funktion dann kopiert werden müsste ) statt einem void trim( string& str ).
Ich mags nämlich nicht, wenn freie Funktion meine Daten so einfach ändern können

-
dann halt:inline std::string trim(std::string const& string) { return string.substr(string.find_first_not_of(" "), string.find_last_not_of(" ") + 1); }
-
(D)Evil schrieb:
dann halt:inline std::string trim(std::string const& string) { return string.substr(string.find_first_not_of(" "), string.find_last_not_of(" ") + 1); }
Was wenn eins string::npos liefert :p
Was hat das
zu bedeuten ?Edit: die smiley's wollen irgendwie nicht ...
-
Airdamn schrieb:
bool is_whitespace(char c) { return c == '\r' || c == '\n' || c == '\t' || c == ' '; }Ähm... nimm
std::isspace( )aus<cctype>.greetz, Swordfish
-
fängst du auch nicht ab ... war nur auf DEINEN Code bezogen ... nciht wie ich das machen würde.
-
Ich habe nirgends nen Code gepostet
Dachte nur wenn (D)evil etwas postet, dann kommt was gutes. Naja brechen wir hier ab ...