Home > Software design >  how to remove something from a string [closed]
how to remove something from a string [closed]

Time:09-29

so, I have an string right here

"test///"

and I want to remove

"///"

from that string

I have tried to use this

str.replace

but it didn't work. so can someone help?

CodePudding user response:

The question is very broad. But basically, you do not want to replace something in your string, but you want to remove or erase the trailing slashes.

There are so many solutions that it is not possible to show the right one.

But if you want to stick to "replace" in a flexible manner, you could use function std::regex_replace

You can define, what will be replaced with a std::regex, which will give you big flexibility.

Example:

std::string output = std::regex_replace(input, std::regex("///"), "");

As said, this is one of millions of possible solutions

CodePudding user response:

You can try the following example code:

  string str1="test///";
  string str2 = str1.substr (0,str1.length()-3);

This will make a substring by removing the last three characters and store it in new string str2. However, you can change the number of characters to remove as per your need.

  •  Tags:  
  • c
  • Related