Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [C++] Trim string

  1. #1

    [C++] Trim string

    Vorrei rimuovere il carattere " dalla mia stringa, la quale può contenerlo solo come carattere iniziale e/o finale:

    codice:
    "aaaa" ---> aaaa
    Come posso fare utilizzando i metodi standard o magari la libreria boost::string?

    Grazie.
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  2. #2
    Effettivamente ho risolto con

    codice:
    boost::trim_if(str, is_any_of("\""));
    o:
    codice:
    boost::erase_all(str, "\"");
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  3. #3
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Puoi usare std::remove(), che in realtà sposta il carattere da togliere in fondo alla stringa e restituisce l'iteratore corrispondente a quel carattere.
    A questo punto puoi riassegnare l'intervallo con .assign() o effettuare un .resize sulla stessa stringa.

    codice:
    string a ="\"aaaa\"";
    // std::string::iterator  it  per il C++98
    auto it = std::remove(a.begin(),a.end(),'\"');
    
    a.assign(a.begin(),it);
    
    oppure
    
    a.resize(std::distance(a.begin(),it));
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  4. #4
    Grazie
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.