Hi, I'm wondering how to remove a specific part of a string..

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str1 = "How,Are";
    cout << str1;
    //remove the comma, and everything behind the comma! so the output would only be 'Are'.
    return 0;
}

Thank you, I am still learning, but also getting the hang of strings as I work this more!

Edited 5 Years Ago by Tom_Weston: n/a

Try googling C++ string or check this link

http://www.cplusplus.com/reference/string/string/erase/

Here's an example

#include <iostream>
#include <string>
#include <iterator>

int main(int argc, char**argv)
{
  std::string str("This is the string to work with");
  
  std::cout << str << std::endl;
  
  std::string::iterator begin = str.begin();
  std::string::iterator end = str.begin() + 8;
  
  str.erase(begin, end);
  
  std::cout << str << std::endl;
  return 0;
}

Edited 5 Years Ago by gerard4143: n/a

Heres the thing, I don't want to remove part of the string with values (example +1, -7). Isn't there a way I can find the comma (,) within the string and remove it and whats before it?

Sure, just find what position the comma resides and then erase everything up to it.

Sure, just find what position the comma resides and then erase everything up to it.

And how would this be done?

If you not comfortable with iterators then try

#include <iostream>
#include <string>
#include <iterator>

int main(int argc, char**argv)
{
  std::string str("This is the string to work with");
  
  std::cout << str << std::endl;
 
  str.erase(0, 8);
  
  std::cout << str << std::endl;
  return 0;
}

Hi, I'm wondering how to remove a specific part of a string..

Thank you, I am still learning, but also getting the hang of strings as I work this more!

Heres the thing, I don't want to remove part of the string with values (example +1, -7). Isn't there a way I can find the comma (,) within the string and remove it and whats before it?

Yes there is. Read a little more about strings.

And how would this be done?

I would recommend rereading the stuff you are "getting the hang" of and start attempting to solve the problem. You also need to stop telling us you have no intention of doing it yourself and you want us to do it for you. If that's not what you meant, sorry, but that is exactly what your second and third post is telling us.

The Member Rules state you need to post an attempt to get help. The code you posted attempted nothing.

This post helped me thank you!

Edited 4 Years Ago by Xaviorin: actually posted in wrong thread

This article has been dead for over six months. Start a new discussion instead.