Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a character from a string

Tags:

c++

string

i have a string. I want to delete the last character of the string if it is a space. i tried the following code,

str.erase(remove_if(str.begin(), str.end(), isspace), str.end());

but my g++ compiler gives me an error saying:

error: no matching function for call to ‘remove_if(__gnu_cxx::__normal_iterator<char*,
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, <unresolved overloaded function type>)’

please help.

like image 549
Prasanth Madhavan Avatar asked Dec 27 '10 10:12

Prasanth Madhavan


People also ask

How to remove characters from a string in Java?

Java Remove Character from String. Java String class has various replace () methods. We can use this to remove characters from a string. The idea is to pass an empty string as a replacement. Let’s look at the replace () methods present in the String class. replace (char oldChar, char newChar): This method returns a new string where oldChar is ...

How do I remove a specific character using find and replace?

Below are the steps to remove a specific character using Find and Replace: Select the range of cells you want to work with. Click on Find & Select from the Home tab (under the ‘Editing’ group). This will display a dropdown menu. Select ‘Replace’. This will open the Find and Replace dialog box.

How do I remove a character from a list in Excel?

The two methods we used for removing a single character can handle a sequence of characters equally well. To remove specific text from each cell in a selected range, press Ctrl + H to display the Find and Replace dialog, and then: Enter the unwanted text in the Find what box. Leave the Replace with box blank.

How to remove characters from a string using regex in Python?

The Regex.Replace () function is used to replace two texts in a specified order. We can remove characters from a string by replacing each character with a string.Empty by specifying each character in the regex pattern parameter.


1 Answers

The first problem is that isspace has multiple overloads in the C++ Standard Library. The initial fix is to provide an explicit type for the function so that the compiler knows which function to take the address of:

#include <string>
#include <algorithm>
#include <cctype>

int main()
{
   std::string str = "lol hi innit";
   str.erase(std::remove_if(str.begin(), str.end(), (int(*)(int))isspace), str.end());
   std::cout << str; // will output: "lolhiinnit"
}

It's a big ugly but, hey, this is C++.

Second, your code will remove all spaces in the string, which is not what you seem to want. Consider a simple if statement on the last character of the string:

#include <string>
#include <cassert>

int main()
{
   std::string str = "lol hi innit ";
   assert(!str.empty());

   if (*str.rbegin() == ' ')
      str.resize(str.length()-1);

   std::cout << "[" << str << "]"; // will output: "[lol hi innit]"
}

Hope this helps.

like image 196
Lightness Races in Orbit Avatar answered Sep 21 '22 22:09

Lightness Races in Orbit