Get all text after point

So i want to get text after a point. I get text form a txt file like this
string filename = "C:/Users/Tim/Desktop/Folder/Documents/Programing/C++/New Folder/Areas/StartingRoom.txt";


ifstream myfile( filename.c_str() );
getline(myfile, firstline);
and check the first letter liek this
if (command[0] == '/'))
but after / i want to get the text. Then loop through variable until that text is found.
1
2
3
4
5
inline std::string get_all_text_after( const std::string& str, char delimiter )
{
    std::size_t pos = str.find(delimiter) ;
    return pos != std::string::npos ? str.substr( pos+1 ) : "" ;
}
Ok thanks but i honesly have no clue how to integrate that into anything...
?
Please?
Ok. With the string::find how do i get were it ends not were it starts?
http://www.cplusplus.com/reference/string/string/find_last_of/

Learning how to search and locate information in documentation / man pages is an important part of learning programming.
Ok thanks,. I tried this and it worked
size_t found;
cout << "Splitting: " << str << endl;
found=str.find_last_of("Get");
cout << " file: " << str.substr(found+1) << endl;
but i tried typing GetHello
and it returned GetH always not Get then Hello
I know its getting after a specific part but im not sure where
I'm not at all clear about what is it that you are trying to do.

To extract the parts of a string separated by a delimiter:

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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

std::vector<std::string> split( const std::string& str, char delimiter )
{
    std::vector<std::string> tokens ;

    std::istringstream stm(str) ;
    std::string tok ;
    while( std::getline( stm, tok, delimiter ) ) tokens.push_back(tok) ;

    return tokens ;
}

int main()
{
    std::string filename = "C:/Users/Tim/Desktop/Folder/Documents/Programing/"
                           "C++/New Folder/Areas/StartingRoom.txt" ;

    std::vector<std::string> parts = split( filename, '/' ) ;

    for( const std::string& p : parts ) std::cout << p << '\n' ;
}
Im trying to get everything after "Get"
Then you shouldn't use string::find_first_of() but string::find()
ok the problem i had is i want to get everything after and it returns the point that get starts. And the command would be GetSomething but it would just retun 0 because thats were Get starts.
> i want to get everything after and it returns the point that get starts.

You should be able to do this on your own. Really.

Once you know where 'get' starts, add three to it and you get where everything after 'get' starts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>

std::string get_everything_after_first( const std::string& str, const std::string& txt )
{
     std::size_t pos = str.find(txt) ;
     return pos != std::string::npos ? str.substr( pos + txt.size() ) : "" ;
}

std::string get_everything_after_last( const std::string& str, const std::string& txt )
{
     std::size_t pos = str.rfind(txt) ;
     return pos != std::string::npos ? str.substr( pos + txt.size() ) : "" ;
}

int main()
{
    const std::string str = "01234<get>56789<get>abcd<get>efgh" ;
    const std::string txt = "get" ;

    std::cout << str << '\n' ;
    std::cout << get_everything_after_first( str, txt ) << '\n' ;
    std::cout << get_everything_after_last( str, txt ) << '\n' ;
}
How could i use cin to set value of str?
Last edited on
> could i use a string for the variables not a const?

Try it. Write a small program and see if it works.

And don't forget to tell us what you learned from that little experiment.
How could i use cin to set value of str? Or a const. I edited the post over yours as you sayd your other one.
Last edited on
anyone please?
Topic archived. No new replies allowed.