Haris Haris - 3 months ago 29
C Question

C++ Get String between two delimiter String

Is there any inbuilt function available two get string between two delimiter string in C/C++?

My input look like


And my output should be


Thanks in advance...


You can do as:

string str = "STARTDELIMITER_0_192.168.1.18_STOPDELIMITER";
unsigned first = str.find(STARTDELIMITER);
unsigned last = str.find(STOPDELIMITER);
string strNew = str.substr (first,last-first);

Considering your STOPDELIMITER delimiter will occur only once at the end.


As delimiter can occur multiple times, change your statement for finding STOPDELIMITER to:

unsigned last = str.find_last_of(STOPDELIMITER);

This will get you text between the first STARTDELIMITER and LAST STOPDELIMITER despite of them being repeated multiple times.