Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all non-alpha characters from string

Tags:

Is there a way how to remove all non-alpha characters (i.e. ,.?! etc) from std::string while not deleting czech symbols like ščéř? I tried using:

std::string FileHandler::removePunctuation(std::string word) {
    for (std::string::iterator i = word.begin(); i != word.end(); i++) {
        if (!isalpha(word.at(i - word.begin()))) {
            word.erase(i);
            i--;
        }
    }
    return word;    
}

but it deletes czech characters.

In the best case, I'd like to use toLowerCase for those symbols too.

like image 554
Jakub Gruber Avatar asked Dec 21 '16 13:12

Jakub Gruber


People also ask

How do you remove characters other than alphanumeric?

We can remove non-alphanumeric characters from the string with preg_replace() function in PHP. The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.

How do I remove all non alphabetic characters in a string python?

Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String. We can use the isalnum() method to check whether a given character or string is alphanumeric or not. We can compare each character individually from a string, and if it is alphanumeric, then we combine it using the join() function.

How do you remove all non-alphanumeric characters from a string excel?

Select the range that you need to remove non-alphanumeric characters from, and click Kutools > Text > Remove Characters. 2. Then a Delete Characters dialog box will appear, only check Non-alphanumeric option, and click the Ok button. Now all of the non-alphanumeric characters have been deleted from the text strings.

How do you get rid of non-alphanumeric?

Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.


2 Answers

You can use std::remove_if along with erase:

#include <cctype>
#include <algorithm>
#include <string>
//...
std::wstring FileHandler::removePunctuation(std::wstring word) 
{
    word.erase(std::remove_if(word.begin(), word.end(), 
                  [](char ch){ return !::iswalnum(ch); }), word.end());
    return word;
}
like image 156
PaulMcKenzie Avatar answered Sep 26 '22 16:09

PaulMcKenzie


Here's an idea:

#include <iostream>
#include <cwctype>
// if windows, add this: #include <io.h>
// if windows, add this: #include <fcntl.h>

int main()
{
  // if windows, add this: _setmode( _fileno( stdout ), _O_U16TEXT );
  std::wstring s( L"š1č2é3ř!?" );
  for ( auto c : s )
    if ( std::iswalpha( c ) )
      std::wcout << c;
  return 0;
}
like image 23
zdf Avatar answered Sep 26 '22 16:09

zdf