How can I read a string within a string without using substr?

void ReadStrInStr()
{
    string Str;
    Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    //cout "A-J" in Str
}

Recommended Answers

All 10 Replies

Look at each character until you find the first character in the find-string. Then check the following characters with the rest of the find-string.
If a mismatch is seen, you haven't found a substring. Continue looking, starting where the first character was found.

Come on, you can do better than that. This isn't rocket science.

I, personally, would create another function and pass both the pointer to the original string, a desired starting point and a desired length (just like the parameters of a substring function) and return a result that is the array of characters between the start value and the requested length.

I, personally, would create another function and pass both the pointer to the original string, a desired starting point and a desired length (just like the parameters of a substring function) and return a result that is the array of characters between the start value and the requested length.

You mean a function like substr() :-P
Which brings me to my question at the OP: Why would you want this?

Substr is too slow.

Usually, questions like this are purely academic; homework.
If you really want something faster, use assembly language.

If substr() was really a problem, someone would have SURELY rewritten it by now.

Maybe a design change is in order so the user or system can only send you the exact string that is to be used.
Maybe the input string could contain delimiters, so you could parse it without substring.

commented: Indeed +14

This what I am really doing, I am loading huge files that are about 100MB to 100GB, create a string to store the file contents, and parse through it. Using substr will be slow because substr is creating new strings - if I am correct.

So you have a file that is 100MB large and you want to search it for a specific string? how are you storing yor string in your function? do you store a new string for each line in the file or are you putting the entire file into one string? what do you want to do with the string that you find the matching substring in?

It really depends on the data.

The problem might be in the size of the string created from the substring and how much of it is passed.
If you're using a pointer to a string, it might not be an issue.
If the files are all just free-form text, it's going to be slow.
If the files have some structure to them, is it possible to load the data as structs or objects?

Also, your long-term goal for the data might be something to consider.
Is some of this going in a database or will it always be in huge files?

Is there a chance to get to the files before they become too large?
Is there a chance to get to the source of the file before it becomes a file?

Tygawr (no one else!!!!), what does substr() do? Please explain. And how does it help you

parse through it [the big string].

???

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.