Searching a string for data between symbols

Hi , this one should be fairly simple I am unsure of the syntax to be able to get the data I am looking for between two symbols in a string, for example. I have a string which stores the following text

 
This is the string of text $Iwantthis+notneeded text, end of the string.


On that string I just wrote out, all I want is the data between $ and + so in theory I should get Iwantthis as a return.

I can use string.find("$")

This finds me the start of the data I need and which char number in the array it is but I am unsure of the syntax to say grab everything after that $ symbol up to the symbol + and then throw that into a variable either as another string or an int if it contains numbers.

Cheers,

Ben.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>

std::string extract(const std::string& str, char beg, char end)
{
    std::size_t begPos ;
    if ( (begPos = str.find(beg)) != std::string::npos )
    {
        std::size_t endPos ;
        if ( (endPos = str.find(end, begPos)) != std::string::npos && endPos != begPos+1 )
            return str.substr(begPos+1, endPos-begPos-1) ;
    }

    return std::string() ;
}

int main()
{
    std::string original("This is the string of text $Iwantthis+notneeded text, end of the string.") ;
    std::cout << extract(original, '$', '+') << '\n' ;
}


http://ideone.com/fJzIEf
Thank you Cire worked perfectly :) , fastest / most efficient way to turn the result string into an int outside of the extract function ? is going to be numbers there sometimes instead of just text.

Also if the same string had two of the same symbols how would I search from the next $ symbol? for example:

string original("This is the string of text $Iwantthis+$Iwantthistoo text, end of the string.") ;

I cant use

cout << extract(original, '$', '+') << '\n' ;
cout << extract(original, '$', ' ') << '\n' ;

the second cout statement will just print $Iwantthis+$Iwantthistoo instead of Iwantthistoo
Last edited on
Hmm its almost like I need to use original.rfind("$"); to look for the last $ symbol in the string and set that as the starting point for the extract function. Any ideas on how to do this or something similar?
Last edited on
Hmm its almost like I need to use original.rfind("$"); to look for the last $ symbol in the string and set that as the starting point for the extract function. Any ideas on how to do this or something similar?


From the standpoint of simplicity you could just make the string parameter fed to the function a non-const reference to string and remove the substring from the original altogether. That would be a subtly different effect from what you describe, though. Imagine the effect on a string that looked like:

"XxXx $Iwant$this+$Iwantthistoo xXxXX"

The method you described would extract "this+$Iwantthistoo" as the second extraction. Whereas the method I described would make the string: "XxXx $Iwantthistoo xXxXX" after the first extraction and the second extraction would be "Iwantthistoo"

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

std::string extract(std::string& str, char beg, char end)
{
    std::size_t begPos ;
    if ( (begPos = str.find(beg)) != std::string::npos )
    {
        std::size_t endPos ;
        if ( (endPos = str.find(end, begPos)) != std::string::npos )
        {
            std::string result = str.substr(begPos+1, endPos-begPos-1) ;
            str.erase(begPos, endPos-begPos+1) ;
            return result ;
        }
    }

    return std::string() ;
}

int main()
{
    std::string original("This is the string of text $Iwantthis+$Iwantthistoo text, end of the string.") ;
    std::cout << extract(original, '$', '+') << '\n' ;
    std::cout << extract(original, '$', ' ') << '\n' ;
    std::cout << "Original modified to \"" << original << "\"\n" ;
}


http://ideone.com/hQ6BZi

Of course, there are other ways to do this. You could, for instance, pass in a reference to an offset that is used by the function to determine where to begin the search and modified by the function to the position of the end character found during the function call. Or maybe you want to go a different route altogether and use a regex implementation.


Last edited on
This work great , exactly what I needed, thank you! :)

I dont fully understand whats going on in that extract function but for now I have what I need to continue working so thats excellent. Would you be able to break down peice by peice whats happening in that function , if you dont have the time or just dont want to I understand.

Thanks for the help again!
begPos is the index into the string where the first character is found.

The first if statement checks if the first character was actually found; if so, we enter the condition.

endPos is the index into the string where the end character is found somewhere after the first character.

The second if statement checks to see if we can find the end character somewhere after the first one. If we do, we enter the block.

Then we create a new string to return from the original string starting at begPos+1 (so we don't include the first character) and going until we hit the end character (the expression is used because we need to pass a number of characters to go).
Topic archived. No new replies allowed.