I have several strings in a text file and I want to search each string for a word starting with "<" and ending with ">". I then want to replace everything from < to > with a new string. So here's a couple lines I want to convert:

400110:	0c12ac0c 	jal	4ab030 <__do_global_ctors_aux>
40019c:	10400003 	beqz	v0,4001ac <call_gmon_start+0x1c>

*note the different string lengths.
So I'm using a line like this to look for "<" within a string:

if( ... test.find("<")!=string::npos ... ){ ...

Is there a good way to find the start and finish of my < to > word without explicitly defining an iterator to traverse the string starting at the point where "<" was found until a ">" is found? This is doable but i have a feeling there is some sort of string function that allows one to do this.

if you use strings' find_first_of() to locate the < you can store the position of the < in a variable and then call find_first_of() for > starting at position + 1.

size_t firstPos = 0, secondPos;
string temp = "4ab030 <__do_global_ctors_aux>";
string sub;
if ((firstPos = temp.find_first_of("<", firstPos)) != string.npos)
    if ((secondPos = temp.find_first_of(">", firstPos + 1)) != string.npos)
        sub = temp.substr(firstPos, (secondPos - firstPos + 1))

I went with this:

test.replace(test.find("<"),test.length(),wordMap[test.c_str()]);

because I know that ">" will always be the end of the string. Also in this case, "<" only occurs once on each line so it works out okay. That's an interesting method though, I'll try it out and see how it works.

Oh, David, maybe you're looking for this syntax "!=string::npos"

@ Dave Not sure why you are getting a compiler error. It runs fine on my machine. Just in case there is a typo this the the exact code that I created that does work.

#include <iostream>
#include <string>

int main()
{
    size_t firstPos = 0, secondPos;
    std::string temp = "4ab030 <__do_global_ctors_aux>";
    std::string sub;
    if ((firstPos = temp.find_first_of("<", firstPos)) != std::string.npos)
        if ((secondPos = temp.find_first_of(">", firstPos + 1)) != std::string.npos)
            sub = temp.substr(firstPos, (secondPos - firstPos + 1));
    std::cout << sub;
    std::cin.get();
    return 0;
}

@ dansnyderECE I wasn't sure if the tags could be anywhere so I came up with this. Since there only at the end your solution works just fine.

This could be very useful if I didn't use getline. Say I have just a large vector full of words or something. I could process each word without requiring as much intra-word associations as in my current program.

That could be a possibility. You would have to run them side by side to see if there is a difference.

dansnyderECE was right. I had to change std::string.npos to std::string::npos I am using g++ 4.4, it must be a compiler difference making it work for you and not for me.

Dave

Well I looked at some other code I have written and I used std::string::npos in it so I guess MSVC++ 2005 will take it either way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.