How can I read a string within a string without using substr?
void ReadStrInStr()
{
string Str;
Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//cout "A-J" in Str
}
For the large-file, "avoid making new strings" problem that raised, the practical choices are: treat subranges as non‑owning views, or avoid loading the whole file into an allocating std::string in the first place. 's character-scan idea and 's pointer+length suggestion are both the right direction — the modern C++ way to get the same behavior without copies is std::string_view (C++17) or boost::string_ref on older toolchains.
std::string big = /* file contents */;
std::string_view whole(big);
std::string_view token(whole.data() + start, len); // no allocation, just a view
// use token for comparisons/parsing; copy to std::string only if you must own it Notes and cautions: the view does not own storage — it is valid only while big remains unchanged and alive. If the underlying buffer can reallocate or be unmapped, the view becomes invalid. For pre‑C++17 code, the same pattern can be done with a const char* + length and algorithms that accept iterator/pointer ranges.
For truly large inputs (100MB–100GB) avoid putting the whole file into one std::string. Use memory-mapped files or stream the file in fixed-size chunks and search with an overlap equal to the pattern length minus one so matches crossing chunk boundaries are found. When doing streaming searches, use an efficient pattern matcher (KMP or a Boyer–Moore variant) if you’ll be searching repeatedly. Only copy the matched substring into a new std::string when you need ownership or a long-lived buffer (answering and : the copy is what costs; views and pointer/length APIs avoid it).
Jump to Post— WaltP 2,905Look 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.
Jump to Post— MandrewP 60Come on, you can do better than that. This isn't rocket science.
Jump to Post— thines01 401Usually, 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 …
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.
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].
???
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.