Get all text after point

May 18, 2012 at 3:20am
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.
May 18, 2012 at 4:39am
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 ) : "" ;
}
May 18, 2012 at 11:56am
Ok thanks but i honesly have no clue how to integrate that into anything...
May 18, 2012 at 8:42pm
?
May 19, 2012 at 1:11am
Please?
May 19, 2012 at 1:53am
May 19, 2012 at 3:19am
Ok. With the string::find how do i get were it ends not were it starts?
May 19, 2012 at 3:27am
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.
May 19, 2012 at 3:44am
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
May 19, 2012 at 4:50am
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' ;
}
May 19, 2012 at 1:35pm
Im trying to get everything after "Get"
May 19, 2012 at 2:17pm
Then you shouldn't use string::find_first_of() but string::find()
May 19, 2012 at 2:39pm
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.
May 19, 2012 at 3:40pm
> 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' ;
}
May 19, 2012 at 4:20pm
How could i use cin to set value of str?
Last edited on May 19, 2012 at 4:34pm
May 19, 2012 at 4:34pm
> 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.
May 19, 2012 at 8:56pm
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 May 19, 2012 at 11:07pm
May 20, 2012 at 2:40am
anyone please?
Topic archived. No new replies allowed.