Remove First and Last Character C++
Asked Answered
E

4

62

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?

Egarton answered 23/5, 2014 at 16:52 Comment(3)
That code will go boom if the string is empty.Bulla
What type is m_VirtualHostName?Hoboken
@Bulla so i must validate first, if VirtualHostName is not empty.Egarton
G
102

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...

Garnes answered 23/5, 2014 at 16:57 Comment(4)
it will crash, if im only have 2 characters ?Egarton
@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).Garnes
@PutraFajarHasanuddin It will when you have no or 1 character in the string. It's documented here. Only execute the second line when size() returns at least 1.Tseng
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());Underbelly
G
11

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.

Galang answered 19/8, 2018 at 20:14 Comment(0)
E
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));
}
Eatables answered 31/7, 2022 at 23:54 Comment(0)
P
-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 ;
    } 
}
Povertystricken answered 5/1, 2019 at 22:45 Comment(4)
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 ...............Povertystricken
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.Jesher
you can put the two condition in same while : as two condtion using AND or one while with if condition with break .Povertystricken
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.Coati

© 2022 - 2024 — McMap. All rights reserved.