1

I'm trying to convert .fsp files to strings but new .fsp file is very abnormal. It contains some undesirable characters that I want to delete from string. How can I make it?

I have tried to search char in string and delete them but I dont know how to make it.

The string looks like this:

string s;

s = 144˙037˙412˙864;

and I need to make it just like that

s = 144037412864;

So I except result like this:

string s = 144037412864;

Thank you for help.

SCouto
  • 7,808
  • 5
  • 32
  • 49
Adam
  • 151
  • 8
  • Did you try a solution of e.g. [How to find and replace string?](https://stackoverflow.com/questions/5878775/how-to-find-and-replace-string) or of the other existing questions about that topic? And why didn’t they solve your problem? – t.niese May 29 '19 at 06:02
  • Possible duplicate of [How to find and replace string?](https://stackoverflow.com/questions/5878775/how-to-find-and-replace-string) – t.niese May 29 '19 at 06:05

2 Answers2

4

We can use the remove-erase idiom to remove unnecessary characters from the string! There's a function in <algorithm> called remove_if. What remove_if does is it removes elements that match some predicate. remove_if returns a iterator pointing to the new end of the container after all elements have been removed. I'll show you how to write a function that does the job!

#include <algorithm>
#include <string>

void erase_ticks(std::string& s) {
    // Returns true for characters that should be removed
    auto condition = [](char c) { return c == '`'; }; 

    // Removes characters that match the condition, 
    // and returns the new endpoint of the string
    auto new_end = std::remove_if(s.begin(), s.end(), condition); 

    // Erases characters from the new endpoint to the current endpoint
    s.erase(new_end, s.end()); 
}

We can use this in main, and it works just as expected!

#include <iostream>

int main() {
    std::string s("123`456`789"); 
    std::cout << s << '\n'; // prints 123`456`789
    erase_ticks(s);
    std::cout << s << '\n'; // prints 123456789
}
Alecto Irene Perez
  • 10,321
  • 23
  • 46
2

This problem has two parts, first we need to identify any characters in the string which we don't want. From your use case it seems that anything that is not numeric needs to go. This is simple enough as the standard library defines a function std::isdigit (simply add the following inclusion "#include <locale>") which takes a character and returns a bool which indicates whether or not the character is numeric.

Second we need a way to quickly and cleanly remove all occurrences of these from the string. Thus we can use the 'Erase Remove' idiom to iterate through the string and do what we want.

string s = "123'4'5";
s.erase(std::remove_if(s.begin(), s.end(), [](char x)->bool {return !std::isdigit(x);}), s.end()); 

In the snippit above we're calling erase on the string which takes two iterators, the first refers to where we want to begin to delete from and the second tells the call where we want to delete to. The magic in this trick is actually all in the call to remove_if (include "#include <algorithm>" for it). remove_if actually works by shifting the elements (or characters) of string forward to the end of the string.

So "123'4'5'" becomes "12345'''", then it returns an iterator to where it shifted these characters to which is then passed to erase to tell it remove the characters starting here. In the end we're left with "12345" as expected.

Edit: Forgot to mention, remove_if also takes a predicate here I'm using a lambda which takes a character and returns a bool.