views:

78

answers:

3

How can I get a string that is between two other declared strings, for example:

String 1 = "[STRING1]" String 2 = "[STRING2]"

Source: "832h0ufhu0sdf4[STRING1]I need this text here[STRING2]afyh0fhdfosdfndsf"

How can I get the "I need this text here" using C++.

A: 

Use strstr http://www.cplusplus.com/reference/clibrary/cstring/strstr/ , with that function you will get 2 pointers, now you should compare them (if pointer1 < pointer2) if so, read all chars between them.

Rin
What if `pointer1` is `NULL`, but `pointer2` is not-`NULL`?
dreamlax
That solution opens you up to a lot of problems. "[String2] XX [String1] YY [String2]" springs to mind.
Martin York
+6  A: 

Since this is homework, only clues:

  • Find index1 of occurrence of String1
  • Find index2 of occurrence of String2
  • Substring from index1+lengthOf(String1) (inclusive) to index2 (exclusive) is what you need
    • Copy this to a result buffer if necessary (don't forget to null-terminate)
polygenelubricants
+2  A: 

Learn what a regular expression is.
Now have a look at boost::regex

Martin York