2012-10-02 23 views

Respuesta

24

De esta manera:

str.erase(0,10); 

...

+0

ugh sí que funciona. Intenté str.erase (str.begin(), str.end() + 10); que me dio un accidente también antes <. < ¡Gracias! – PTS

+3

str.begin(), str.begin() + 10 – Arkadiy

+0

Más rápido que str = str.substr (10) como 8:11 en cadenas grandes. –

4

Uso std::string::substr:

try { 
    str = str.substr(10); 
} catch (std::out_of_range&) { 
    //oops str is too short!!! 
} 
  1. http://www.cplusplus.com/reference/string/string/substr/
+0

Gracias también funciona bien sin chocar, pero el segundo es un poco más corto. – PTS

+1

@Paul es más corto al escribir, pero debo decir que str.erase (size_t, size_t) tiene un mejor rendimiento en su caso. – PiotrNycz

+0

Es bueno saber que toda mi aplicación requiere bastante tiempo. – PTS

1

sospecho que hay aquí hay más código que no muestra, y es probable que el problema esté allí.

Este código funciona bien:

#include <string> 
#include <iostream> 

using namespace std; 

int main(int argc, char **argv) 
{ 
    string imgURL = "<img src=\"http://imgs.xkcd.com/comics/sky.png"; 

    string str = imgURL; 
    int urlLength = imgURL.length(); 
    urlLength = urlLength-10; 
    str.erase (str.begin(), str.end()-urlLength); 
    imgURL = str; 

    cout << imgURL << endl; 

    return 0; 
} 

Dicho esto, hay formas más cortas de hacer esto, como otros han mencionado.

Cuestiones relacionadas