55

How to remove first and last character from std::string, I am already doing the following code.

But this code only removes the last character

m_VirtualHostName = m_VirtualHostName.erase(m_VirtualHostName.size() - 1)

How to remove the first character also?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Putra Fajar Hasanuddin
  • 1,101
  • 3
  • 13
  • 25

4 Answers4

93

Well, you could erase() the first character too (note that erase() modifies the string):

m_VirtualHostName.erase(0, 1);
m_VirtualHostName.erase(m_VirtualHostName.size() - 1);

But in this case, a simpler way is to take a substring:

m_VirtualHostName = m_VirtualHostName.substr(1, m_VirtualHostName.size() - 2);

Be careful to validate that the string actually has at least two characters in it first...

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • it will crash, if im only have 2 characters ? – Putra Fajar Hasanuddin May 23 '14 at 17:00
  • @Putra: Yes. C++ is not particularly friendly that way ;-) **Edit**: No, I misread your comment. It will crash if you have *less than* two characters (0 or 1). – Cameron May 23 '14 at 17:02
  • 1
    @PutraFajarHasanuddin It will when you have no or 1 character in the string. It's documented [here](http://en.cppreference.com/w/cpp/string/basic_string/erase). Only execute the second line when [`size()`](http://en.cppreference.com/w/cpp/string/basic_string/size) returns at least 1. – Appleshell May 23 '14 at 17:03
  • 13
    While the `substr` looks cleaner in code it will trigger an additional memory allocation (which might or not be important). Also the first erase might be simpler if stated as: `str.erase(str.end()-1); str.erase(str.begin());` – David Rodríguez - dribeas May 23 '14 at 18:09
9

My BASIC interpreter chops beginning and ending quotes with

str->pop_back();
str->erase(str->begin());

Of course, I always expect well-formed BASIC style strings, so I will abort with failed assert if not:

assert(str->front() == '"' && str->back() == '"');

Just my two cents.

Hernán
  • 4,527
  • 2
  • 32
  • 47
0

Simple answer:

str = str.substr(1,str.length()-2);

And to further what others were saying about @mhadhbi_issam code, here is a better approach:

void trimmed(std::string &str)
{
  int begI = 0,endI = str.length();
  if (endI == begI)
    return;
  std::string::iterator beg = str.begin();
  std::string::iterator end = str.end();
  end--;
  while (isspace(*beg) || isspace(*end))
  {
    if (isspace(*beg)) { beg++; begI++; }
    if (isspace(*end)) { end--; endI--; }
  }
  str = str.substr(begI,(endI-begI));
}
CRB
  • 111
  • 1
  • 6
-1
std::string trimmed(std::string str ) {
if(str.length() == 0 ) { return "" ; }
else if ( str == std::string(" ") ) { return "" ; } 
else {
    while(str.at(0) == ' ') { str.erase(0, 1);}
    while(str.at(str.length()-1) == ' ') { str.pop_back() ; }
    return str ;
    } 
}
Mhadhbi issam
  • 197
  • 3
  • 6
  • this function make the string trimmed , it is equivalent to QString::trimmed(QString) , it delete the white space from end and front , do whatever it need to modify it to make it do the job ............... – Mhadhbi issam Jan 05 '19 at 22:50
  • The while-loops (especially the front one) will make this quite inefficient. Search for the last/first blank and execute a single erase for each. – Brandlingo Mar 15 '19 at 12:41
  • you can put the two condition in same while : as two condtion using AND or one while with if condition with break . – Mhadhbi issam Mar 16 '19 at 13:08
  • Ok, so not only is this a code-snipped without any explanation or comment on why one would write it like that, this also does NOT do what was asked but also the action it performs is horribly inefficient. For those wondering: This code would do SINGLE erases on the front of the string until there is no leading space, then do single erases on the pack of the string.... a trim-function that is really really slow. – ABaumstumpf Jul 06 '22 at 07:26