how to ignore the rest of the string

I need a function that ignore reading the rest of a string
For example if a character is "/" I want to ignore reading it and what comes after it till the end of the string .

There are many ways to solve this. I show just two examples below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a = " adfh//ghjl";
    
    size_t pos = a.find('/'); // look for specific character
    if (pos != string::npos)  // if it is found
    {
        a = a.substr(0, pos); // select substring starting at character - and of length pos
    }
    cout << "a = (" << a << ")\n";
    
    
    
    
    string b = " adfh?ghjl";

    pos = b.find_first_of(".,\\;!?/#");  // look for any of these characters
    if (pos != string::npos)             // if any are found
    {
        b.resize(pos);                   // change size of string - discarding rest.
    }
    cout << "b = (" << b << ")\n";
    
    
}

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/substr/
http://www.cplusplus.com/reference/string/string/find_first_of/
http://www.cplusplus.com/reference/string/string/resize/

Edit:
the first suggestion I made (above) is a more clumsy version of the pretty neat solution suggested in the other thread:
http://www.cplusplus.com/forum/general/207180/#msg978400

Last edited on
Topic archived. No new replies allowed.